07-23-2023, 04:41 AM
Say I have an enum,
public enum Colours
{
Red,
Blue
}
The only way I can see of parsing them is doing something like:
string colour = "Green";
var col = (Colours)Enum.Parse(typeOf(Colours),colour);
This will throw a [System.ArgumentException][1] because "Green" is not a member of the `Colours` enum.
Now I really hate wrapping code in try/catch's, is there no neater way to do this that doesn't involve me iterating through each `Colours` enum, and doing a string comparison against `colour`?
[1]:
public enum Colours
{
Red,
Blue
}
The only way I can see of parsing them is doing something like:
string colour = "Green";
var col = (Colours)Enum.Parse(typeOf(Colours),colour);
This will throw a [System.ArgumentException][1] because "Green" is not a member of the `Colours` enum.
Now I really hate wrapping code in try/catch's, is there no neater way to do this that doesn't involve me iterating through each `Colours` enum, and doing a string comparison against `colour`?
[1]:
[To see links please register here]