I'm very new to Solidity and running into this issue.
As you can see in this picture, I'm trying to set up a scenario where there exists 1 state variable called text. Then, I want to run a transaction that calls doStuff. doStuff creates a copy of text and stores it in testString. I then want to modify this testString in another function, so I indicate a "pass by reference" using the storage keyword next to the parameter called _text.
Obviously this doesn't work because I'm getting an error that I can't implicitly cast a string memory to a string storage. So my question is: how do I create a copy (by using memory), and then pass that copy by reference to be modified (using storage)? What is the best practice/workaround here? Any comments about how to properly use memory vs storage would also be greatly appreciated.
Here is the code:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Jacob {
string text;
function doStuff() public view returns (string memory) {
string memory testString = text;
updateTestString(testString);
return (testString);
}
function updateTestString(string storage _text) private view {
_text = "Hello, World!";
}
}
updateTestString
, which takes a storage reference of a string, and 2- you can't set a a string literal ("Hello, World!") to a storage reference of a string, you'll need assembly for that low-level control.text
, just do it directly. The following line only results in the copying of the string data from storage to memory, it's copying the string value, not the storage reference:string memory testString = text;