Is it possible to return a string from a function ?

JonasMaubertJonasMaubert Member Posts: 2
edited June 2016 in Solidity
I have some issues trying to return a string with a function, when the first function has to return the string from a 2nd function.
Here some details :
contract A {
    string myString = "someString";

    function getMyString() returns (string thatString) {
        return myString;
    }
}
this works perfectly. But this :
contract A {
    string myString = "someString";

    function getMyString() returns (string thatString) {
        return myString();
    }
}

contract B {
    function getMyStringFromA(address contractA) returns (string thatString) {
        return A(contractA).myString();
    }
}
It doesn't work.
I wasn't neither able to find doc about string in solidity, like if it's possible to concatenate 2 strings or not.
I'have spend the last day on this problem, I don't understand where is my mistake, if you have any tips for me, it would be great !
Thank you.

Comments

  • ZikileminiZikilemini Member Posts: 9
    This is strange indeed. Have you tried with the string initialised in a constructor ?
  • HippoClipHippoClip Member Posts: 16
    edited July 2016
    Returning a string is possible. Have you tried not naming the returned string? I.e.,
    
    function getMyString() returns (string) {
            return myString();
        }
    
  • JonasMaubertJonasMaubert Member Posts: 2
    I found out that a function cannot return the result of a second function, if the result of this second function isn't a finish value. And a string isn't a finish value (a string can be longer than 32 or 64 bytes).
    The only thing you can do is return a Bytes32 which is a finish value. (that's what I did).
    Or you can return the length of the string, and then extract every bytes one by one. But if you use Js to speak with your chain, this doesn't work. (I do use Js).
    Hope this will help someone with the same problem i had, because i lost a lot of time with it ;)
Sign In or Register to comment.