當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET Enum類代碼示例

本文整理匯總了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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:48,代碼來源:Enum

示例2:

Public Enum ArrivalStatus As Integer
   Late = -1
   OnTime = 0
   Early = 1
End Enum
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:5,代碼來源: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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:6,代碼來源:Enum

輸出:

Arrival Status: OnTime (0)

示例4: CType

Dim value3 As Integer = 2
Dim status3 As ArrivalStatus = CType(value3, ArrivalStatus)

Dim value4 As Integer = CInt(status3)
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:4,代碼來源:Enum

示例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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:21,代碼來源:Enum

輸出:

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)
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:3,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:21,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:5,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:8,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:7,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:4,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:9,代碼來源: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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:4,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:6,代碼來源:Enum

輸出:

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
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:41,代碼來源:Enum

輸出:

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.


注:本文中的System.Enum類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。