[Ethereum] How to access public variables, and getting return values in smart contracts


Say I have a smart contract such as:

contact Rabbit {
    string public name;

    function setName(string n) returns (bool) {
        name = n;
        return true;

How can I get the value of name after having set it? I'm using Truffle. I've tried (where rabbit is a handle of Rabbit:


This just returns ''.

Also, how can I get the return value? If I execute setName I just see the transaction, not the return value. I read somewhere I can use call(), such as: rabbit.setName.call("barry"), though it wont save the transaction so it needs ot be call again, without call(). Have I got this wrong?


Best Answer

Insertion can be done using sendTransaction function.

 Rabbit.deployed().then(function(contract) {contract.createProperty("Hello",{from: web3.eth.accounts[0]}).then(function(v) {console.log(v)})})

You can use call method to get the value of public variable.Try the below command in truffle console.

 Rabbit.deployed().then(function(contract)  {contract.name.call().then(function(v) {console.log(v)})})
Related Topic