0
contract B {
  struct Player {
    uint id;
  } 
  mapping (uint=> Player) Players; 
}

contract A {
  constructor() public {
    B.Player storage p = B.Players[1];
  }
}

Report err at B.Players[1]:Member "Players" not found or not visible after argument-dependent lookup in type(contract B).

So, how can I use mapping Players in A from B?

  • 1
    You cannot access private variables of other contracts. The variables need to be public if you want others to access them. – Mikko Ohtamaa Jan 30 '19 at 10:20
  • contract B { struct Player { uint id; } mapping (uint=> Player) public Players; } contract A { constructor() public { B.Player storage p = B.Players[1]; } } – xu mo Jan 30 '19 at 10:31
  • its the same err – xu mo Jan 30 '19 at 10:32
  • I think the issue here is that you're trying to access contract state, which is only possible when you're referring to an object of type B, not to the contract B itself. An analogy would be that the contract is like a blueprint for a house, and just as you cannot access the property "owner" for a blueprint, but only when you construct a house that an owner is accessible, similarly only when you have an object of type B, that you can access it's storage member variable Players[1] etc. – Bharat Mallapur Dec 7 '19 at 4:52
1

You can not directly access variables of other contracts. One of the way that you have to inherit contract B for using its variable.

contract B {    
    struct  Player {
        uint id;   
    }
    mapping (uint=> Player) public Players; 
 }

contract A is B {
    constructor() public {
        B.Player storage p = B.Players[1];   
    }
 }
  • Inherit will make my code too long(over 1000 line) then out of gas.I'm finding the way that I can access variables in B contract. – xu mo Jan 31 '19 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.