-
In address ethUsdPriceFeed = helperConfig.activeNetworkConfig(); However, in contract HelperConfig is Script {
NetworkConfig public activeNetworkConfig;
struct NetworkConfig {
address priceFeed;
}
......
} In my point of view, So why is there a pair of brackets at the end of I tried to write in other ways, but all of them got error. Just like some other Objective-Oriented languages, I attempted to write address ethUsdPriceFeed = helperConfig.activeNetworkConfig.priceFeed; The error is
ChatGPT told me that "Solidity interprets the brackets as a function call. The brackets () are used to indicate that you want to access a specific field within the returned struct." It suggests that I should write like this: address ethUsdPriceFeed = helperConfig.activeNetworkConfig().priceFeed; However, the error is the same as above. In the video 50:30, Patrick mistakenly code as the last one under the recommendation of Copilot. He just said an excuse-me but didn't explain why. I would appreciate it very much if someone can tell me why only the first code is right and how exactly I can access the element of a struct in Solidity. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
All the state variables that are public, for them solidity automatically makes their getter function with the same name as that of variable. And to access the members for the activeNetworkConfig you have to write like this
Suppose the struct NetworkConfig struct looks like.
Then you can access it in DeployFundMe.s.sol by:
|
Beta Was this translation helpful? Give feedback.
-
solidity automatically makes little getter functions for the variables you store so you can access them externally. address ethUsdPriceFeed = helperConfig.activeNetworkConfig().priceFeed; trying to access the 'priceFeed' part of your NetworkConfig type variable doesn't work above cos the getter function for this has a simple end goal: to return the entire struct. It doesn't let you choose what to access before doing that. struct NetworkConfig {
address priceFeed; // ETHUSD price feed
uint chainId; // chain ID
}
.....
NetworkConfig memory anvilConfig = NetworkConfig(address(mockPriceFeed), block.chainid);
return anvilConfig;
(address ethUsdPriceFeed, uint chainId) = helperConfig.activeNetworkConfig(); The getter will return the entire struct. Only after can you choose which elements to use. (, uint chainId) = helperConfig.activeNetworkConfig(); On the contrary, if you are calling the struct from inside the originating contract, you can access it that way without a problem: activeNetworkConfig.priceFeed
activeNetworkConfig.chainId Cos while inside the contract we are accessing the actual struct and not just a pointer to it |
Beta Was this translation helpful? Give feedback.
solidity automatically makes little getter functions for the variables you store so you can access them externally.
trying to access the 'priceFeed' part of your NetworkConfig type variable doesn't work above cos the getter function for this has a simple end goal: to return the entire struct. It doesn't let you choose what to access before doing that.
If you change HelperConfig.sol by adding a second element to the config like so..