[Ethereum] How to access string arguments from calldata in external functions? (0.5.X)

calldatasolcsolidity

With the introduction of Solc 0.5.X , parameter memory locations for arrays (and strings/bytes) must be made explicit. For external functions, this memory location is required to be specified as calldata (as per docs).

My question is, how to convert the following function to be 0.5.X compatible and access the properties from within the function body. In addition, what changes (if any) will this have to how we call the method and pass the args?

0.4.X

function accessTheString(string _uri) 
external {        
    contractString = _uri;
}

0.5.X

function accessTheString(string calldata) 
external {        
    // check for && access the string
}

Best Answer

The format is

function accessTheString(string calldata _uri) external{        
    string memory contractString= _uri;
}

Now, I think you made a mistake while calling the function by without passing string between " ". You have to call function accessTheString, by passing a string enclosed in " " ex: accessTheString("hello");

I tested this in remix, and its working

Related Topic