3
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?

4
  • 1
    You cannot access private variables of other contracts. The variables need to be public if you want others to access them. Commented Jan 30, 2019 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
    Commented Jan 30, 2019 at 10:31
  • its the same err
    – xu mo
    Commented Jan 30, 2019 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. Commented Dec 7, 2019 at 4:52

2 Answers 2

2

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];   
    }
 }
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
    Commented Jan 31, 2019 at 2:15
0

You can use interface to use the datatype in both contracts

interface IPerson{
  struct Name{
     string firstName;
     string lastName;
  }

  
}

contract Admin{
   add(IPerson.Name name){
   
   }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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