0
    pragma solidity ^0.4.19;

    contract Bank{
    uint private value;

    function Bank(uint amount){
        value = amount;
    }
    function deposit(uint amount){
        value += amount;
    }
    function withdraw(uint amount){
        value -= amount;
    }
    function balance() returns (uint){
        return value;
    }
}

contract MyFirstContract is Bank(10)
{
    string private name;
    uint private age;

    function setName(string newName){
        name = newName;
    }
    function getName()returns(string){
        return name;
    }
    function setAge(uint newAge){
        age = newAge;
    }
    function getAge() returns (uint) {
        return age;
    }
}

I am executing my contract it compiles successfully. I have passed default value inside my bank constructor and then i get the balance but it is showing me blank.

Thanks in advance.

1

It is because you are setting the visibility of "value" to private

As per the doc :

private: Private functions and state variables are only visible for the contract they are defined in and not in derived contracts.

I tried in remix by changing the visibility to public and I got the correct value .

pragma solidity ^0.4.19;

contract Bank {

    uint public value;

    function Bank(uint amount){
        value = amount;
    }
    function deposit(uint amount){
        value += amount;
    }
    function withdraw(uint amount){
        value -= amount;
    }
    function balance() returns (uint){
        return value;
    }
}

contract MyFirstContract is Bank(10) {

    string private name;
    uint private age;

    function setName(string newName){
        name = newName;
    }
    function getName()returns(string){
        return name;
    }
    function setAge(uint newAge){
        age = newAge;
    }
    function getAge() returns (uint) {
        return age;
    }
}

And while creating an instance of MyFirstContract (in remix using as well javascript vm) it gives me the result "10" when I'm calling balance .

 status     0x1 Transaction mined and execution succeed
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     browser/ballot.sol:MyFirstContract.balance() 0xef55bfac4228981e850936aaf042951f7b146e41
 gas    3000000 gas
 transaction cost   21798 gas 
 execution cost     526 gas 
 hash   0xaf38d98f04f66d38034ae2b88922eaa00c2f487610fa00f32c17e1fbc6e0cd7f
 input  0xb69ef8a8
 decoded input  {}
 decoded output     {
    "0": "uint256: 10"
}
 logs   []
 value  0 wei
  • Could you share the exact code you used to get this working? When I use Remix and just change the declaration of value to uint public value, it still seems to hold the value 0 (according to calls to balance() as well as inspecting via the public getter for value). Note that I'm using the JavaScript VM. – smarx Dec 12 '17 at 14:38
  • Sure I've updated my post . – tbrunain Dec 12 '17 at 14:50
  • I don't think that the visibility of amount has anything to do with this working; see my answer. For me, balance() returns the correct value even when amount is private (as long as the right contract is deployed). – smarx Dec 12 '17 at 14:53
1

Make sure you're deploying the right contract.

Your code defines two contracts: Bank and MyFirtContract. I find that Remix defaults to Bank (perhaps based on the order the contracts are defined, or perhaps simply alphabetically?), and deploying Bank of course results in a value of 0. When I deploy MyFirstContract with the exact code in your post, I find that balance() returns 10 as expected.

(You can choose which contract to deploy via a dropdown in the "run" tab.)

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.