How I can use a state variable which is in another contract(contract_a) in my contract(contract_b). That variable is public. I just want to use some special variables not all data that are in contract_1.
-
you can inherit: https://stackoverflow.com/questions/70643149/call-a-function-in-another-contract-solidity/70643414#70643414 – Yilmaz Aug 05 '22 at 13:12
2 Answers
First answer here, I wish it'll be helpful.
It looks like every variable of a contract has an implicit getter method, which at first I thought was a little unusual. When you call this variable from another contract, you are calling its getter method. So, instead of calling car.color, you've got to call car.color().
I'm still learning, so DYOR.

- 1
- 2
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 18 '22 at 13:04
When you use the import statement in a contract you import all the functions and all the variables of the smart contract you are importing.
In your contractB you need to have an instance of the contractA (or its address) and then call through this instance the variable you want to access. E.g:
import "./ContractA.sol"
contract ContractB {
ContractA instanceOfA;
function callA() public {
instanceOfA.variableYouWantToAccess();
}
}
Note the parentheses () after the name of the variable you want to access, that is because Solidity, for all the varibles, specifies a getter function which is the function you call in order to access these varibles.

- 85
- 9