2

I have two contract and in first contract it contains a function which is responsible for returning two values, one of string and one of integer.

function getUserData(address userAddress)
    public    constant    returns(string, uint)
  {
    string Firstname = "Hello";
    uint number=123;
   return( Firstname,number);
  }

On compiling this gives error "Type literal_string "Hello" is not implicitly convertible to expected type string storage pointer. string Firstname = "Hello"; " In the second contract I make object of first contract and tried to get function

function FetchDetail(address userAddress) public constant returns(string,uint )
  {
    string memory  data1;
    uint  data2;
    UserRegistration ud=new UserRegistration();
   (data1,data2)= ud.getUserData(userAddress);
    return(data1,data2);
  }

But I am unable to get values. Please suggest me.

  • 3
    The compilation error can be resolved with string memory Firstname = "Hello";. – smarx Feb 21 '18 at 15:11
1

Since Firstname is a string literal defined in the function scope, missing to explicitly define the data location sets it to storage by default (where the state variables are persisted) [1]. That assignment operation then looks for the data at the provided location, which in the example is not a valid storage reference. In order to solve the compilation error in the provided example, it is necessary to set the memory keyword, as pointed out by user smarx: string memory Firstname = "Hello";.

The second snippet is valid, as of Solidity version 0.4.25.

Given the provided code snippets, the following would be the aggregated code, to serve as reference (SecondContract.sol):

pragma solidity ^0.4.25;

contract UserRegistration {
    function getUserData(address userAddress) public constant returns(string, uint) {
        string memory Firstname = "Hello";
        uint number=123;
        return( Firstname,number);
    }
}

contract SecondContract {
    function FetchDetail(address userAddress) public constant returns(string,uint ) {
        string memory  data1;
        uint  data2;
        UserRegistration ud=new UserRegistration();
        (data1,data2)= ud.getUserData(userAddress);
        return(data1,data2);
    }
}

Deploying SecondContract and calling FetchDetail function with 0x0000000000000000000000000000000000000000 as argument returns:

string Hello uint256 123

[1] https://solidity.readthedocs.io/en/v0.4.25/types.html#data-location

Stay super!

/Javi

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.