0

Is there a way to access variables in solidity using another variable?

Something like this:

contract Test {
    uint age;
    string varAge = "age";

    function setAge() public {
        // varAge should be able to access age variable
       ${varAge} = 30;
    }
}
Savvy Sage
  • 347
  • 1
  • 4
  • 12
  • Why do you need to do it like that? Can you specify the use case? Maybe some other clean solution exists. – adzo261 Apr 28 '22 at 13:11

1 Answers1

0

No. It's not like PHP.

The closest thing I can think of is having a mapping like mapping(string=>uint) myMap;. Then you can do stuff like myMap["abc"] = 123;

You could also do this...

uint key = "abc";
myMap[key] = 123;
flcoder
  • 713
  • 4
  • 14