VB.Net/Language Basics/Enum

Материал из VB Эксперт
Перейти к: навигация, поиск

Convert Element in Enum to Integer

Imports System
public class MainClass
     Enum Temperatures
         CelsiusMeetsFahrenheit = -40
         WickedCold = 0
         FreezingPoint = 32
         LightJacketWeather = 60
         SwimmingWeather = 72
         BoilingPoint = 212
     End Enum
    Shared Sub Main()
         Console.WriteLine("Freezing point of water: {0}",CType(Temperatures.FreezingPoint, Integer))
         Console.WriteLine("Boiling point of water: {0}",CType(Temperatures.BoilingPoint, Integer))
    End Sub
End Class


Convert Enum element to int and call ToString

Imports System
Imports System.Data
Imports System.Collections
public class MainClass
   Shared Sub Main()
        Dim I1, I2, I3 As Integer
        I1 = E.A : I2 = E.B : I3 = E.C
        Console.WriteLine(I1.ToString() &" " & I2.ToString() &" " & I3.ToString())
   End Sub
End Class
    Enum E
        A = 5
        B
        C = 6   " B and C will both be 6
    End Enum


Convert Enum to Int

Imports System
Public Class MainClass
    Enum Temperatures
       WickedCold = 0
       FreezingPoint = 32
       LightJacketWeather = 60
       SwimmingWeather = 72
       BoilingPoint = 212
    End Enum "Temperatures
    Shared Sub Main(ByVal args As String())
       Console.WriteLine("Freezing point of water: {0}",CInt(Temperatures.FreezingPoint))
       Console.WriteLine("Boiling point of water: {0}", CInt(Temperatures.BoilingPoint))
    End Sub
End Class


Enum data Demo

Imports System
Public Class MainClass
    Private Enum Names As Integer
        Norman = 1
        Mike = 2
        Reece = 3
    End Enum
    Shared Sub Main()
       System.Console.WriteLine(Names.Norman.ToString & " = " & Names.Norman)
       System.Console.WriteLine(Names.Mike.ToString & " = " & Names.Mike)
       System.Console.WriteLine(Names.Reece.ToString & " = " & Names.Reece)
    End Sub
End Class


Enum Temperatures

Imports System
Public Class MainClass
    Enum Temperatures
       WickedCold = 0
       FreezingPoint = 32
       LightJacketWeather = 60
       SwimmingWeather = 72
       BoilingPoint = 212
    End Enum "Temperatures
    Shared Sub Main(ByVal args As String())
       Console.WriteLine("Freezing point of water: {0}",Temperatures.FreezingPoint)
       Console.WriteLine("Boiling point of water: {0}", Temperatures.BoilingPoint)
    End Sub
End Class


Use Enum to define Access Level

Imports System
Imports System.Diagnostics
Public Class MainClass
   Public Enum AccessLevel
        Clerk
        Supervisor
        Administrator
        User = Clerk
        Manager = Supervisor
        SysAdmin = Administrator
        Superuser = Administrator
    End Enum
    Shared Sub Main()
        Dim access_level As AccessLevel = AccessLevel.SysAdmin
        Dim txt As String = access_level.ToString() & vbCrLf
        txt &= AccessLevel.Clerk.ToString() & vbCrLf
        txt &= AccessLevel.Supervisor.ToString() & vbCrLf
        txt &= AccessLevel.Administrator.ToString() & vbCrLf
        
        Console.WriteLine(txt)
    End Sub
End Class