How to create an enumeration of constants with and without explicit values in VB.NET

2 Answers

0 votes
Imports System

Public Class Test
    Enum EN
      VBNET = 12
      C = 23
      PHP = 43      
      Python = 75
    End Enum
    
    Public Shared Sub Main()
        Console.WriteLine(EN.PHP)    
        Console.WriteLine(EN.PHP.ToString)  
    End Sub
End Class


' run:

' 43
' PHP

 



answered Mar 24, 2019 by avibootz
0 votes
' 
' Title: Enumeration of Constants in VB.NET
' Example with and without explicit values
'

Imports System

' ---------------------------------------------
' Enum WITHOUT explicit values
' VB.NET automatically assigns 0, 1, 2...
' ---------------------------------------------
Enum Color
    Red     ' 0
    Green   ' 1
    Blue    ' 2
End Enum

' ---------------------------------------------
' Enum WITH explicit and mixed values
' ---------------------------------------------
Enum Status
    OK = 1          ' 1
    Warning = 5     ' 5
    Err           ' 6 (auto: previous + 1)
    Critical = 10   ' 10
End Enum

Module Program
    Sub Main()
        Console.WriteLine("Enum without explicit values:")
        Console.WriteLine("Red = " & CInt(Color.Red))
        Console.WriteLine("Green = " & CInt(Color.Green))
        Console.WriteLine("Blue = " & CInt(Color.Blue))

        Console.WriteLine()
        Console.WriteLine("Enum with explicit and mixed values:")
        Console.WriteLine("OK = " & CInt(Status.OK))
        Console.WriteLine("Warning = " & CInt(Status.Warning))
        Console.WriteLine("Err = " & CInt(Status.Err))
        Console.WriteLine("Critical = " & CInt(Status.Critical))
    End Sub
End Module



' run:
'
' Enum without explicit values:
' Red = 0
' Green = 1
' Blue = 2
'
' Enum with explicit and mixed values:
' OK = 1
' Warning = 5
' Err = 6
' Critical = 10
'

 



answered Apr 26 by avibootz

Related questions

...