2

I think I'm missing something very simple. The following contract

contract Genobject{
uint public item;
function Genobject(uint _val){
    item=_val;}
}

contract Caller{
   Genobject public instance;
   function Caller(){
       instance= new Genobject(3);
   }
   function Recall() returns (uint){
      return Genobject.item; }
}

Gives this error

MultiConstructor.sol:27:16: Error: Member "item" not found or not visible after argument-dependent lookup in type(contract Genobject)

    return Genobject.item;
           ^------------^

All my types are the same, and "item" is declared publicly so it should be readable. What's going wrong?

Thanks in advance

user1938620
  • 681
  • 1
  • 6
  • 12

1 Answers1

1

Here's your code, modified:

pragma solidity ^0.4.4;

contract Genobject {
    uint public item;

    function Genobject(uint _val) {
        item = _val;
    }
}

contract Caller {
   Genobject public instance;

   function Caller() {
       instance = new Genobject(3);
   }

   function recall() constant returns (uint) {
        return instance.item();
   }
} 

And following is a screenshot of Browser Solidity demonstrating the execution of your contract:

enter image description here

Here are the changes I made:

  1. Renamed Recall() to recall().

  2. The recall() function is marked as constant as this function does not modify data, but returns the data.

  3. The recall() function returns the item() value of the instance variable rather than the class Genobject.

  4. item is accessed in recall() by the automatically generated item() public function.

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • Is lower case for functions just a style guide or does it have larger ramifications? Similarly, if I accidentally left out the "constant" keyword would this introduce the possibilities of bugs or is it just to make things read easier? Thanks – user1938620 Jan 27 '17 at 14:53
  • Lowercase for functions (except for the constructor which must match the class name) is more a style guide. Leaving out constant means that the function is one that modifies data on the blockchain, and therefore will have to be executed as a blockchain transaction rather than a call that just reads the blockchain data. And when your execute a transaction, you get back a transaction hash that you later query to get the transaction details. – BokkyPooBah Jan 27 '17 at 15:01