Visual Basic .NET - Enum
VB.NET Enum is a keyword that stands for "Enumerations," which is a data type that allows programmers to define a set of named constants. Each constant is assigned a unique integer value that can be used in the program. VB.NET Enums are often used to make the code more readable and maintainable. Instead of using hard-coded values, programmers can use Enums to give meaningful names to values.
Syntax:
Enum
…
End Enum
Here is an example of how to declare an Enum in VB.NET:
Enum DaysOfWeek
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
End Enum
In the example above, the Enum is called "DaysOfWeek," and it contains seven named constants.
How to use VB.NET Enum:
To use the VB.NET Enum, you first need to declare it in your code. Once declared, you can use it to define variables, constants, and parameters. Here is an example of how to use the Enum:
Dim today As DaysOfWeek
today = DaysOfWeek.Tuesday
In this example, we declared a variable called "today" of type DaysOfWeek. We then assigned the value Tuesday to the "today" variable using the DaysOfWeek.Tuesday syntax.
VB.NET Enum with Integer Value:
By default, the first constant in an Enum is assigned the value of 0, and each subsequent constant is assigned a value that is one greater than the previous constant. However, you can also assign your own values to the constants in the Enum. Here is an example of how to do this:
Enum WeekDays
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
End Enum
In this example, we assigned the constant Monday the value of 1, Tuesday the value of 2, and so on.
VB.NET Enum Methods:
VB.NET Enums also have some useful methods that can be used to work with the values in the Enum. These include:
- GetName: Returns the name of the constant with the specified value.
- GetNames: Returns an array of strings that contains the names of all the constants in the Enum.
- GetValues: Returns an array of values that contains all the values of the constants in the Enum.
Here is an example of how to use these methods:
Dim days() As String = System.Enum.GetNames(GetType(DaysOfWeek))
Dim values() As Integer = System.Enum.GetValues(GetType(DaysOfWeek))
In this example, we used the System.Enum class to get an array of strings that contains the names of all the constants in the DaysOfWeek Enum, and an array of integers that contains the values of all the constants in the DaysOfWeek Enum.
Module Module1
Enum Color
Red
Blue
Green
End Enum
Sub Main()
Dim myColor As Color
myColor = Color.Red
Select Case myColor
Case Color.Red
Console.WriteLine("The color is red.")
Case Color.Blue
Console.WriteLine("The color is blue.")
Case Color.Green
Console.WriteLine("The color is green.")
End Select
Console.ReadLine()
End Sub
End Module
In this example, we define an Enum called Color that has three possible values: Red, Blue, and Green. We then declare a variable myColor of type Color and assign it the value Red.
We use a Select Case statement to check the value of myColor and print out a message depending on which value it has. In this case, since myColor is Red, the output will be "The color is red."
Enums are a powerful tool in programming because they allow you to define a set of named values that can be used throughout your code. This can make your code more readable and easier to maintain, since you can refer to these values by name rather than using hard-coded numbers or strings.