Cannot pass string from one contract into another contract

desktopdandesktopdan Member Posts: 15
I have this contract (nameRegister) that is creating another contract (name) and therefore calling its constructor with some strings.

contract name {
string public firstname;
string public lastname;

function name (string _firstname, string _lastname) {
firstname =_firstname;
lastname =_lastname;
}
}

contract nameRegister {

function nameRegister () {
}

function copyName (address _existingNameId){
name existing_name = name(_existingNameId);
var newname_address = new name (existing_name.firstname(), existing_name.lastname());
// var newname_address = new name ("a", "b"); --- this compiles
}

}
If you just pass in some strings then it compiles.

But if you get these strings from an existing contract and pass them in, it does not like it and produces this error

string_problem.sol:21:37: Error: Invalid type for argument in function call. Invalid implicit conversion from inaccessible dynamic type to string memory requested.
var newname_address = new name (existing_name.firstname(), existing_name.lastname());
^-----------------------^
string_problem.sol:21:64: Error: Invalid type for argument in function call. Invalid implicit conversion from inaccessible dynamic type to string memory requested.
var newname_address = new name (existing_name.firstname(), existing_name.lastname());
^----------------------^
I have tried all sorts of ways of extracting the values from the existing contract and passing them along, but nothing works.

Can anyone explain what is going on here and how you can get around it?

Thanks!

Answers

  • desktopdandesktopdan Member Posts: 15
    Upon further investigation I came across these posts that explain the fact that a contract cannot currently read a string that is obtained from another contract. I found this very useful

    http://ethereum.stackexchange.com/questions/3727/contract-reading-a-string-returned-by-another-contract/

    If you change the above contracts so that strings become bytes32 it will work. It just depends how much string data you need to store. In my case, probably 32 bytes is long enough!

    contract name {

    bytes32 public firstname;
    bytes32 public lastname;

    function name (bytes32 _firstname, bytes32 _lastname) {
    firstname =_firstname;
    lastname =_lastname;
    }

    }


    contract nameRegister {

    function nameRegister () {
    }

    function copyName (address _existingNameId){
    name existing_name = name(_existingNameId);
    var newname_address = new name (existing_name.firstname(), existing_name.lastname());
    }

    }
Sign In or Register to comment.