Enum.TryParse() substitute

  • Thread starter Thread starter John A Grandy
  • Start date Start date
J

John A Grandy

Primitives like Int32 provide a Parse() method , and TryParse() method --
which is very useful.

Enum provides a Parse() method, but not a TryParse() method.

Other than wrapping the Enum.Parse() in an exception handler , can someone
recommend a technique similar to TryParse() ?

Thanks.
 
John said:
Primitives like Int32 provide a Parse() method , and TryParse() method --
which is very useful.

Enum provides a Parse() method, but not a TryParse() method.

Other than wrapping the Enum.Parse() in an exception handler , can someone
recommend a technique similar to TryParse() ?

Thanks.
Messy but:
int EnumValue;
If(Int.TryParse("1"))
{
MyEnum x = (MyEnum)Enum.Parse(EnumValue);
}

This requires EnumValue is a valid value for MyEnum otherwise it will
get a invalidcastexception.
Wrong it appears, it will work fine but x will still not be a value of
MyEnum.

JB
 
Back
Top