-1

Smart contract

pragma solidity ^0.4.11;

contract PassData{


    PassData passData;
    string s;
    function PassData(){
        s="come";
    }

    function getData() returns(string){
        return s;
    }

    function accessData(address contractAddress) returns(string){
        passData=PassData(contractAddress);
        return passData.getData();
    }


}

Error

browser/Untitled.sol:18:16: Error: Return argument type inaccessible dynamic type is not implicitly convertible to expected type (type of first return variable) string memory. return passData.getData(); ^----------------^

is there any other way to return the string in solidity?

Also tried returns(string memory) but still same error.

I know we can store byte32 and return easily but I want as a string.

2

2 Answers 2

1

Your code has error, i updated, as follows:

pragma solidity ^0.4.11;

contract PassData{


    string s;

    function PassData(){
        s="come";
    }

    function getData() constant returns(string){
        return s;
    }

    function setData(string _string) {
        s = _string;
    }

}

Hope it helps ~

2
  • I don't want to constant getData() function. As my question is there any way to return string variable? Aug 9, 2017 at 3:13
  • The dynamic length string can't be passed between contracts~ Aug 9, 2017 at 3:25
0

In the example you gave, all of accessData() should be running outside the blockchain. Then your contract reduces to:

pragma solidity ^0.4.11;

contract PassData{

  string public s;

  function PassData(){
    s="stored";
  }
}

The web3.js code would run something like:

abi = [{"constant":true,"inputs":[],"name":"s","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]
PassData = web3.eth.contract(abi);
passData = PassData.new();
console.log(passData.s());

Edit~

I was kind of winging the web3.js version above. After the poster mentioned issues creating the contract, I wanted to confirm the exact code, but Python is the only test environment I have up and handy. Directly addressing the "ropsten network" error that comeback4you mentioned in the comment is starting to get pretty far removed from the original question.

In the meantime, here is verified web3.py code, with Python 3:

from web3 import Web3, TestRPCProvider

web3 = Web3(TestRPCProvider())

PassData = web3.eth.contract(
                  contract_name='PassData',
                  bytecode="6060604052341561000f57600080fd5b5b6040805190810160405280600681526020017f73746f72656400000000000000000000000000000000000000000000000000008152506000908051906020019061005b929190610062565b505b610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610197806101166000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806386b714e21461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100925780820151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101635780601f1061013857610100808354040283529160200191610163565b820191906000526020600020905b81548152906001019060200180831161014657829003601f168201915b5050505050815600a165627a7a72305820778bc5b6da7c15f0d757e7d4f6a8d6f83f3bbcab490b7f07214956fed3aa900a0029",
                  abi=[{"constant":True,"inputs":[],"name":"s","outputs":[{"name":"","type":"string"}],"payable":False,"type":"function"},{"inputs":[],"payable":False,"type":"constructor"}],
              )

txid = PassData.deploy()

receipt = web3.eth.getTransactionReceipt(txid)

passData = PassData(receipt.contractAddress)

print(passData.call().s())

which outputs stored

2
  • I tried you code .. I didn't see any result from console.log(). And when I instantiated, eth complains that I should use ropsten network. I have already fired up testrpc, and it even communicated with my migration
    – user618677
    Aug 8, 2017 at 22:00
  • I don't want to public 's' variable. As my question is there any way to return string variable? Aug 9, 2017 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.