Search code examples
ethereuminline-assemblysolidityevm

dynamically create a variable in solidity


I'd like to include an internal method in my contract that allows the creation of a new uint256 in storage named by a parameter. Something like:

function createUint (string memory _name) internal {
    /*
     * creates a new uint256 named _name in storage
     */
}

My guess is that it would require inline assembly, but I can't figure out how


Solution

  • How about a mapping?

    mapping(string => uint256) myMap;
    
    function setValue(string memory name, uint256 value) internal {
        myMap[name] = value;
    }