Welcome to Greg McKinley.com Sign in | Join | Help

Enum.Parse C# and VB.NET

Enum.Parse is easy to remember with C# but I always forget the VB.Net syntax because the Enum keyword is a keyword and a class.

C#

 40     public enum ClickType

 41     {

 42         Submit = 1,

 43         Reset = 2,

 44         None = 0

 45     }

 46 

 47     ClickType eClick;

 48     string sTemp = "Submit";

 49 

 50     eClick = (ClickType) Enum.Parse(typeof(ClickType), sTemp, true);

VB.NET

 263    Public Enum ClickType

 264         Submit = 1

 265         Reset = 2

 266         None = 0

 267     End Enum

 268 

 269     Dim eClick As ClickType

 270     Dim sTemp As String = "Submit"

 271 

 272     eClick = CType([Enum].Parse(GetType(ClickType), sTemp), ClickType)

Published 21-06-2006 08:45 by gmckinley

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

Kürşad said:
One can use "System.Enum.Parse" instead of "[Enum].Parse" if he/she like.
January 10, 2008 7:07 AM
Ekus said:
I think in VB.NET it is better to use DirectCast(object, type) instead of CType when translating casting from C#. CType internally adds try/catch for possible type mismatch, which we don't need/want in most places.
September 23, 2008 4:22 AM

What do you think?

(required) 
(optional)
(required) 
captcha Image