0

I'm learning Solidity and using remix IDE.


My ultimate goal is to store list addresses in one contract and then access them via another contract. But for the sake of simplicity here I consider only one address.

Problem: The address stored in T1 contract is changed when I call it and see it in T2.

pragma solidity ^0.4.4;
contract T1{
   address public a;
   function T1(){
   a=msg.sender;// here I want to store only the owner
  }
} 


pragma solidity ^0.4.4;
import "./T1.sol";

contract T2{
   address public b;
   T1 t1=new T1();
   function T2(){
     b= t1.a(); // here I want to retrive whatever's stored in a, but it 
   //changes
  }
}

Now if you check b, the address is different than I stored in a.

Question: How can I solve the above problem?


Note: I'd like to store the address of people who call contract T1, so I think I have to use msg.sender, not a fixed one, like 0x123.

1

When you do T1 t1=new T1();, you are creating a new contract instance. Apart from the fact that it happens to have been created with the same source code, this isn't connected to your original T1 instance whose address you are hoping to retrieve.

Instead, you want to access the existing contract instance. To do this, you will need to let T2 know the address of T1, for example as an argument to the constructor. Then you can do something like T1 t1 = T1(t1address) and you'll get the existing contract instance, which will have the a that was set earlier when it was mined.

| improve this answer | |
  • thanks for the answer, but T1 t1 = T1(t1address) is not working in remix! – user153465 Aug 17 '17 at 16:11
  • Have you defined t1address? – Edmund Edgar Aug 17 '17 at 22:44
  • could you elaborate on this and explain how to define it. – user153465 Aug 18 '17 at 9:15
  • contract T2 { address t1address; function T2(address _t1address) { t1address = _t1address} etc... } – Edmund Edgar Aug 18 '17 at 21:11

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.