'Use a variable to access key in struct
I have a struct like so :
struct test {
string a;
string b;
}
and a mapping like this:
mapping (address => test) public tests;
I want to have a function to update the struct like this:
function updateStruct (string _paramName, string _newValue) {
tests[msg.sender].(_paramName) = _newValue; // this is the line that shows the logic but doesn't work in compilation
}
How can I do this?
Solution 1:[1]
Solidity currently (v0.8) does not support accessing properties via magic variables.
You'll need to access the properties directly.
function updateStructA(string memory _newValue) public {
tests[msg.sender].a = _newValue;
}
function updateStructB(string memory _newValue) public {
tests[msg.sender].b = _newValue;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Petr Hejda |
