Enumerations (Enums) in VB.NET
An enumeration, or Enum
, is a data type that consists of a set of named integral constants. Enums provide a way to define a set of related named values, making your code more readable and maintainable by replacing "magic numbers" with descriptive names.
Why Use Enums?
- Readability: Replaces hard-coded integer values with meaningful names.
- Maintainability: Easier to update a single definition than to find and replace all occurrences of a value.
- Type Safety: Helps prevent accidental assignment of incorrect values to variables.
- Self-Documenting Code: The names themselves explain the purpose of the values.
Declaring an Enum
You declare an enum using the Enum
keyword. Each member of the enum is assigned a value. If you don't explicitly assign a value, it defaults to the value of the previous member plus one, starting with 0 for the first member.
Public Enum Status
Pending ' Defaults to 0
Processing ' Defaults to 1
Completed ' Defaults to 2
Cancelled ' Defaults to 3
End Enum
Public Enum DaysOfWeek
Sunday = 1
Monday = 2
Tuesday = 3
Wednesday = 4
Thursday = 5
Friday = 6
Saturday = 7
End Enum
Using Enums
Once declared, you can use an enum as a data type for variables and assign its members as values.
Example Usage
Dim currentStatus As Status = Status.Processing
Dim today As DaysOfWeek = DaysOfWeek.Tuesday
If currentStatus = Status.Processing Then
Console.WriteLine("The order is currently being processed.")
End If
Console.WriteLine($"Today is {today.ToString()}")
Enum Underlying Types
By default, enums have an underlying type of Integer
. However, you can specify a different integral type, such as Byte
, Short
, Long
, SByte
, UShort
, UInteger
, or ULong
.
Public Enum ErrorCodes As Byte
NoError = 0
FileNotFound = 1
AccessDenied = 2
End Enum
Flags Attribute
For enums that represent a combination of options, you can use the [Flags]
attribute. This allows you to combine enum members using bitwise operations (Or
).
<Flags>
Public Enum FilePermissions As Integer
None = 0
Read = 1 ' Binary: 0001
Write = 2 ' Binary: 0010
Execute = 4 ' Binary: 0100
All = Read Or Write Or Execute ' Binary: 0111
End Enum
' Example:
Dim myPermissions As FilePermissions = FilePermissions.Read Or FilePermissions.Write
' myPermissions now has the value 3 (Binary: 0011)
If (myPermissions And FilePermissions.Read) = FilePermissions.Read Then
Console.WriteLine("Read permission is granted.")
End If
Enum Member Values and Bitwise Operations
When using the [Flags]
attribute, it's crucial that each enum member has a unique power of two value (1, 2, 4, 8, 16, etc.) to ensure correct bitwise operations.
Common Enum Methods
Enum.GetName(GetType(YourEnum), value)
: Returns the string name of the enum member with the specified value.Enum.Parse(GetType(YourEnum), name)
: Parses a string representation of an enum name and returns the corresponding enum value.Enum.IsDefined(GetType(YourEnum), value)
: Checks if a value is defined for the enum.Enum.GetValues(GetType(YourEnum))
: Returns an array of all possible enum values.Enum.GetNames(GetType(YourEnum))
: Returns an array of all enum member names.