<Flags> Enum Colors As Integer
None = 0
Red = 1
Green = 2
Blue = 4
End Enum
Module Example
Public Sub Main()
Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
For Each colorString As String In colorStrings
Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)
If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then
Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)
End If
Catch e As ArgumentException
Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
End Try
End Sub
End Module
' The example displays the following output:
' Converted '0' to None.
' Converted '2' to Green.
' 8 is not an underlying value of the Colors enumeration.
' Converted 'blue' to Blue.
' Converted 'Blue' to Blue.
' Yellow is not a member of the Colors enumeration.
' Converted 'Red, Green' to Red, Green.
参数
value
包含枚举成员的基础值或命名常量的字符串表示形式,或者由逗号 (,) 分隔的命名常量列表。 中
value
每个值、名称或逗号的前面或后面可以有一个或多个空格。 如果
value
是列表,则返回值是指定名称的值,并结合按位
OR
运算。
如果
value
是与 的命名常量
enumType
不对应的名称,则方法将
ArgumentException
引发 。 如果
value
是不表示枚举的基础值的整数的
enumType
字符串表示形式,则该方法返回其基础值
value
转换为整型类型的枚举成员。 如果不需要此行为,请调用
IsDefined
方法以确保整数的特定字符串表示形式实际上是 的成员
enumType
。 下面的示例定义枚举
Colors
,调用
Parse(Type, String, Boolean)
方法将字符串转换为相应的枚举值,并调用
IsDefined
方法以确保特定整型值是枚举中
Colors
的基础值。
参数
ignoreCase
指定此操作是否区分大小写。
public:
generic <typename TEnum>
where TEnum : value class static TEnum Parse(System::String ^ value, bool ignoreCase);
public static TEnum Parse<TEnum> (string value, bool ignoreCase) where TEnum : struct;
static member Parse : string * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String, ignoreCase As Boolean) As TEnum
TEnum
public:
generic <typename TEnum>
where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value, bool ignoreCase);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value, bool ignoreCase) where TEnum : struct;
static member Parse : ReadOnlySpan<char> * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char), ignoreCase As Boolean) As TEnum
TEnum
public:
generic <typename TEnum>
where TEnum : value class static TEnum Parse(ReadOnlySpan<char> value);
public static TEnum Parse<TEnum> (ReadOnlySpan<char> value) where TEnum : struct;
static member Parse : ReadOnlySpan<char> -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As ReadOnlySpan(Of Char)) As TEnum
TEnum
public:
generic <typename TEnum>
where TEnum : value class static TEnum Parse(System::String ^ value);
public static TEnum Parse<TEnum> (string value) where TEnum : struct;
static member Parse : string -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String) As TEnum
TEnum