Enum Parsing in VB.NET

Parsing enum values from a string is bit pain in the ass in VB.NET, even thought that language can parse enum values from name AND value strings.
The language lacks generic type of implementation for Enum.Parse, which causes extra casting.

Casting required
DirectCast fixes type problem
As seen in example Enum.Parse is not enough, because method is returning object. We can solve this problem by adding DirectCast, but that's just messy. If we try to use the explicit type for parsing, we notice that there is no ExampleEnum.Parse method. We could use the TryParse method, which is more secure way to parse values, but what if we want to just parse the value?

Enum TryParse

We can write simple utility method to help enum parsing. With this helper we can reduce casting.

Enum parse helper
Of T As {Structure} is a generic restriction and it says that given type must be value type. We cannot restrict T into enums only, because language doesn't support that yet. There is hopefully some changes coming up in Roslyn. https://github.com/dotnet/roslyn/issues/262


  1. I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies. Could you please extend them a little from next time?..Keep this great work
    Web development Company in India Company | Web development Company Bangalore
    Web Development Company in Bangalore | Responsive Web development

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru Dot Net Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

  2. It is amazing and wonderful to visit your site. Thanks for sharing this information; this is useful.
    You can also visit here .net training in gurgaon