3
pragma solidity ^0.4.0;

contract SimpleStorage {
    uint storedData;

    function set(uint x) {
        storedData = x;
    }

    function get() constant returns (uint) {
        return storedData;
    }
}

Reference link: http://solidity.readthedocs.io/en/develop/introduction-to-smart-contracts.html#simple-smart-contract

Why it returns 0, instead of 100?

greeter.get() returns 0 instead of 100, any help??

3

You are passing x = 100 to the contract constructor in your console session, but there is no constructor in the contract.

Here is your code:

var greeter1 = greeterContract.new(x, {from: ...

First argument x is considered as a contract constructor argument. See web3 API here for more info.

Try these steps instead:

var greeter1 = greeterContract.new({from: web3.eth.accounts[0], data: code, gas: 1000000})

Send transaction that calls set function on the new contract instance:

greeter1.set(100)

Call get function without sending transaction:

greeter1.get.call()

Please note the difference: the first command creates a transaction but the second does not.

1

Based on the provided screen you are setting local JavaScript variable in the console to 100.

Then you are asking the contract for its variable storedData.

No call to SimpleStorage.set() has been made in this point, so the contract returns the default uninitialized value that is 0.

  • Tq for the theoritical explanation , pl provide practical explanation – NarraVasu Feb 20 '17 at 13:11
  • @NarraVasu: Let's go back to the basics. Do you know the concept of a variable in the programming language and how they work? – Mikko Ohtamaa Feb 20 '17 at 13:57
1
  1. simplestorage_sol_simplestorage.set(100)
  2. if there is error msg shows 'invalid address' then eth.defaultAccount = eth.accounts[0]

  3. simplestorage_sol_simplestorage.set(100)

  4. simplestorage_sol_simplestorage.get()

enter image description here

  • what is simplestorage_sol_simplestorage ? is it compiled code of solc compiler . In my contract on geth console simplestorage_sol_simplestorage compiled code not showing like above . any suggestion? – NarraVasu Feb 21 '17 at 8:52
  • simplestorage_sol_simplestorage is the name of the contract instance name, equals to 'greeter1' in your example. – rocky802 Feb 21 '17 at 9:17
  • Thank you for screenshot , but i might fail at last step at set() value can you provide whole contract screen shot if possible . Thank you – NarraVasu Feb 21 '17 at 10: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.