Các Enum.Parse phương pháp:
Chuyển đổi chuỗi đại diện của tên hoặc giá trị số của một hoặc hằng liệt kê nhiều hơn đến một đối tượng liệt kê tương đương. Tham số chỉ định xem hoạt động có phân biệt chữ hoa chữ thường hay không.
Đây là VB.NET mã ví dụ từ MSDN:
Module Example
Public Sub Main()
Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
For Each colorString As String In colorStrings
Try
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())
Else
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
Next
End Sub
End Module
Nguồn
2009-01-25 00:13:37
bằng cách này, nếu bạn luôn muốn enum bắt đầu với giá trị 0 (zero), bạn không cần phải nói, chỉ BCD là đủ – balexandre
Tôi không biết điều đó, cảm ơn mẹo. – Middletone