本文整理汇总了VB.NET中System.Enum类的典型用法代码示例。如果您正苦于以下问题:VB.NET Enum类的具体用法?VB.NET Enum怎么用?VB.NET Enum使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Enum类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: EnumTest
Public Class EnumTest
Enum Days
Saturday
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
End Enum
Enum BoilingPoints
Celsius = 100
Fahrenheit = 212
End Enum
<Flags()> _
Enum Colors
Red = 1
Green = 2
Blue = 4
Yellow = 8
End Enum
Public Shared Sub Main()
Dim weekdays As Type = GetType(Days)
Dim boiling As Type = GetType(BoilingPoints)
Console.WriteLine("The days of the week, and their corresponding values in the Days Enum are:")
Dim s As String
For Each s In [Enum].GetNames(weekdays)
Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(weekdays, [Enum].Parse(weekdays, s), "d"))
Next s
Console.WriteLine()
Console.WriteLine("Enums can also be created which have values that represent some meaningful amount.")
Console.WriteLine("The BoilingPoints Enum defines the following items, and corresponding values:")
For Each s In [Enum].GetNames(boiling)
Console.WriteLine("{0,-11} = {1}", s, [Enum].Format(boiling, [Enum].Parse(boiling, s), "d"))
Next s
Dim myColors As Colors = Colors.Red Or Colors.Blue Or Colors.Yellow
Console.WriteLine()
Console.WriteLine("myColors holds a combination of colors. Namely: {0}", myColors)
End Sub
End Class
示例2:
Public Enum ArrivalStatus As Integer
Late = -1
OnTime = 0
Early = 1
End Enum
示例3: Main
Public Module Example
Public Sub Main()
Dim status As ArrivalStatus = ArrivalStatus.OnTime
Console.WriteLine("Arrival Status: {0} ({0:D})", status)
End Sub
End Module
输出:
Arrival Status: OnTime (0)
示例4: CType
Dim value3 As Integer = 2
Dim status3 As ArrivalStatus = CType(value3, ArrivalStatus)
Dim value4 As Integer = CInt(status3)
示例5: Example
Public Enum ArrivalStatus As Integer
Unknown = -3
Late = -1
OnTime = 0
Early = 1
End Enum
Module Example
Public Sub Main()
Dim values() As Integer = { -3, -1, 0, 1, 5, Int32.MaxValue }
For Each value In values
Dim status As ArrivalStatus
If [Enum].IsDefined(GetType(ArrivalStatus), value)
status = CType(value, ArrivalStatus)
Else
status = ArrivalStatus.Unknown
End If
Console.WriteLine("Converted {0:N0} to {1}", value, status)
Next
End Sub
End Module
输出:
Converted -3 to Unknown Converted -1 to Late Converted 0 to OnTime Converted 1 to Early Converted 5 to Unknown Converted 2,147,483,647 to Unknown
示例6:
Dim status As ArrivalStatus = ArrivalStatus.Early
Dim number = Convert.ChangeType(status, [Enum].GetUnderlyingType(GetType(ArrivalStatus)))
Console.WriteLine("Converted {0} to {1}", status, number)
输出:
Converted Early to 1
示例7: CType
Dim number As String = "-1"
Dim name As String = "Early"
Dim invalid As String = "32"
Try
Dim status1 As ArrivalStatus = CType([Enum].Parse(GetType(ArrivalStatus), number), ArrivalStatus)
If Not [Enum].IsDefined(GetType(ArrivalStatus), status1) Then status1 = ArrivalStatus.Unknown
Console.WriteLine("Converted '{0}' to {1}", number, status1)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.",
number)
End Try
Dim status2 As ArrivalStatus
If [Enum].TryParse(Of ArrivalStatus)(name, status2) Then
If Not [Enum].IsDefined(GetType(ArrivalStatus), status2) Then status2 = ArrivalStatus.Unknown
Console.WriteLine("Converted '{0}' to {1}", name, status2)
Else
Console.WriteLine("Unable to convert '{0}' to an ArrivalStatus value.",
number)
End If
输出:
Converted '-1' to Late Converted 'Early' to Early
示例8: formats
Dim formats() As String = { "G", "F", "D", "X"}
Dim status As ArrivalStatus = ArrivalStatus.Late
For Each fmt As String In formats
Console.WriteLine(status.ToString(fmt))
Next
输出:
Late Late -1 FFFFFFFF
示例9: names
Dim names() As String = [Enum].GetNames(GetType(ArrivalStatus))
Console.WriteLine("Members of {0}:", GetType(ArrivalStatus).Name)
Array.Sort(names)
For Each name In names
Dim status As ArrivalStatus = CType([Enum].Parse(GetType(ArrivalStatus), name),
ArrivalStatus)
Console.WriteLine(" {0} ({0:D})", status)
Next
输出:
Members of ArrivalStatus: Early (1) Late (-1) OnTime (0) Unknown (-3)
示例10: GetType
Dim values = [Enum].GetValues(GetType(ArrivalStatus))
Console.WriteLine("Members of {0}:", GetType(ArrivalStatus).Name)
For Each value In values
Dim status As ArrivalStatus = CType([Enum].ToObject(GetType(ArrivalStatus), value),
ArrivalStatus)
Console.WriteLine(" {0} ({0:D})", status)
Next
输出:
Members of ArrivalStatus: OnTime (0) Early (1) Unknown (-3) Late (-1)
示例11:
Dim familyPets As Pets = Pets.Dog Or Pets.Cat
If familyPets.HasFlag(Pets.Dog) Then
Console.WriteLine("The family has a dog.")
End If
输出:
The family has a dog.
示例12:
<Flags> Public Enum Pets As Integer
None = 0
Dog = 1
Cat = 2
Bird = 4
Rodent = 8
Reptile = 16
Other = 32
End Enum
示例13:
Dim familyPets As Pets = Pets.Dog Or Pets.Cat
If familyPets And Pets.Dog = Pets.Dog Then
Console.WriteLine("The family has a dog.")
End If
输出:
The family has a dog.
示例14:
Dim familyPets As Pets = Pets.Dog Or Pets.Cat
If familyPets = Pets.None Then
Console.WriteLine("The family has no pets.")
Else
Console.WriteLine("The family has pets.")
End If
输出:
The family has pets.
示例15: Main
' 导入命名空间
Imports System.Runtime.CompilerServices
' Define an enumeration to represent student grades.
Public Enum Grades As Integer
F = 0
D = 1
C = 2
B = 3
A = 4
End Enum
' Define an extension method for the Grades enumeration.
Public Module Extensions
Public minPassing As Grades = Grades.D
<Extension>
Public Function Passing(grade As Grades) As Boolean
Return grade >= minPassing
End Function
End Module
Public Module Example
Public Sub Main()
Dim g1 As Grades = Grades.D
Dim g2 As Grades = Grades.F
Console.WriteLine("{0} {1} a passing grade.",
g1, If(g1.Passing(), "is", "is not"))
Console.WriteLine("{0} {1} a passing grade.",
g2, If(g2.Passing(), "is", "is not"))
Console.WriteLine()
Extensions.minPassing = Grades.C
Console.WriteLine("Raising the bar!")
Console.WriteLine()
Console.WriteLine("{0} {1} a passing grade.",
g1, If(g1.Passing(), "is", "is not"))
Console.WriteLine("{0} {1} a passing grade.",
g2, If(g2.Passing(), "is", "is not"))
End Sub
End Module
输出:
D is a passing grade. F is not a passing grade. Raising the bar! D is not a passing grade. F is not a passing grade.