'How to initialize a static string array in solidity?
I want to declare an array to store my strings as follows.
bytes32[3] verifiedResult;
verifiedResult = ["verified successfully", "verified failed", "not clear"];
Error shows that
ParserError: Expected identifier but got '=' --> contracts/1_Storage.sol:23:20: | 23 | verifiedResult = ["verified_successfully", "verified_failed", "not clear"] |^
what should I do to fix it? Is there any better way to declare a static string array in solidity?
Solution 1:[1]
You can use the string
keyword, or in your case, a fixed-sized (3) array of strings.
pragma solidity ^0.8.4;
contract MyContract {
// string in storage
string[3] verifiedResult = ["verified successfully", "verified failed", "not clear"];
function foo() external {
// string in memory
string[3] memory anotherStringArray = ["verified successfully", "verified failed", "not clear"];
}
}
Also, your copy looks like you can use enum.
pragma solidity ^0.8.4;
contract MyContract {
enum VerifiedResult {VerifiedSuccessfully, VerifiedFailed, NotClear}
VerifiedResult result;
function foo() external {
result = VerifiedResult.NotClear; // stores `NotClear` to the `result` property
}
}
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 | Pang |