Is it possible to modify a variable value from another contract

0 votes

I could get the information about access another contract's variable but I couldn't find how to modify another contract's variable.

Here is the example of contract A,

contract A {
    uint public target;
}

And this is the example of contract B

contract B {
    function edit_A_a() public {
        A.target = 1;  // some kind of this
    }
}

I want to modify the value of target variable from contract B.

Sep 24, 2018 in Blockchain by digger
• 26,740 points
4,345 views

2 answers to this question.

0 votes

Declaring a state variable as public generates a public getter, but not a setter. If you want another contract to modify your contract's state variable, you'll have to write a function to do that yourself:

contract A {
    uint public target;
    function setTarget(uint _target) public {
        target = _target;
    }
}

contract B {
    A a = Test(0x123abc...);  // address of deployed A
    function editA() public {
        a.setTarget(1);
    }
}
answered Sep 24, 2018 by slayer
• 29,370 points
0 votes

No, you can't directly edit a variable of a contract. That would be a security nightmare.

You can only use public/external functions provided by an external contract through interfaces. If that function itself is a setter and allows you to change a variable, only then it is possible.

Contract A:

contract A {
    uint myVariable = 1

    function setMyVariable(uint _newVar) public {
        myVariable = _newVar;
    }
}

Contract B:

interface A {
    function getMyVariable() view public returns(uint);
}

function setMyVariable(uint _newVar) public onlyOwner {
    A a = A([CONTRACT A ADDRESS HERE])
    a.setMyVariable(_newVar);
}
answered Sep 24, 2018 by Sai

Related Questions In Blockchain

+1 vote
1 answer

I would like to create a blockchain network which will stay at the same level of complexity, is it possible?

You can very easily create a cryptocurrency having a ...READ MORE

answered Apr 4, 2018 in Blockchain by Christine
• 15,790 points
849 views
0 votes
1 answer

In a Blockchain, how difficult is it to modify the third to last block?

Technically, it's not difficult at all, all ...READ MORE

answered Apr 20, 2018 in Blockchain by Christine
• 15,790 points
1,027 views
+1 vote
1 answer

Is it possible to store blockchain in a sql or no-sql database?

Currently, following are the options to store ...READ MORE

answered Apr 21, 2018 in Blockchain by Perry
• 17,100 points

edited Aug 9, 2018 by Omkar 1,135 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,923 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
+3 votes
2 answers

How to run ethereumjs using Node.JS

You need to install testrpc globally on ...READ MORE

answered Mar 27, 2018 in Blockchain by ned_crew
• 1,610 points
1,414 views
0 votes
1 answer

Is it possible to send eth to a contract through its constructor from another contract?

I found the solution for this: pragma solidity ...READ MORE

answered Sep 26, 2018 in Blockchain by slayer
• 29,370 points
1,715 views
0 votes
1 answer

How to get return value from solidity contract?

Once you call a constant function, you ...READ MORE

answered Aug 16, 2018 in Blockchain by slayer
• 29,370 points
5,040 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP