Parse a string to an Enum value in VB.NET

后端 未结 4 1617
难免孤独
难免孤独 2020-12-01 01:43

How can I parse a string in VB.NET to enum value?

Example I have this enum:

Public Enum Gender
    NotDefined
    Male
    Female
End Enum

4条回答
  •  情书的邮戳
    2020-12-01 02:28

    If you want the parse to be case insensitive, you can use the following:

    [Enum].Parse(Gender, DirectCast(MyGender, String), True)
    

    This will handle dim MyGender as string = "Male" or dim MyGender as string = "male"

提交回复
热议问题