0

I want to call a method of a contract from another contract. I've already read this post: calling one contract to another contract method, but it doesn't work for me.

When I create the caller and try to execute the Set() method it perfectly works, but if call the Get() it alwayas returns '0'.

Can someone help me?

These are my simple code:

pragma solidity ^0.4.0;


contract callee{

    uint public x;

    constructor() public
    {
        x = 1;
    }

    function set (uint k) public
    {
        x = k;
    }

    function get () public view returns (uint)
    {
        return x;
    }

}

contract caller{

    callee c;

    constructor() public
    {
        c = new callee();
    }

    function get() public view returns (uint k)
    {
        return c.get();
    }

    function set(uint k) public
    {
        c.set(k);
    }

    function get_calle() public view returns (callee)
    {
        return c;
    }


}

I'm using Web3 Provider and Remix to test.

I have tried to call get() from the callee after getting its location, and it returns the right value, so the set() methods works. Why can't I use get() from the caller?

methods invocation from remix using Web3

Thank you!

0

The callee you created isn't the one the caller is interacting with. It's creating a new one.

Replace

c = new callee();

with

c = callee(0xabcdef);

But instead of 0xabcdef put in the address of the deployed callee contract.

  • That's not the point. I want to create a new callee and edit its state. I just deploy the caller contract wich create the callee and then execute the get after the set. As u can see in the image, I'm working on the same callee, but the get() of the caller returns 0 while the get() of the callee returns 10. – Pierz___ Apr 18 '18 at 12:06
  • Ah I understand, my bad. I tested it using Remix, with Injected Web3 on Rinkeby and got the same problem. Could set from caller but only got accurate get result from callee. But when I copied the address of caller, removed caller and reopened an instance at that address, get seems to work. Do you get the same result? – AnAllergyToAnalogy Apr 18 '18 at 12:22
  • I tried, but i always get 0 – Pierz___ Apr 18 '18 at 12:27
  • It's odd, it may be a bug with Remix because as I said, it works for me when I re-launch the caller, and I don't see any issue with your Solidity code. – AnAllergyToAnalogy Apr 18 '18 at 12:45
  • Ok thank you! What should i try? – Pierz___ Apr 18 '18 at 12:49

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.