Tutorial by Examples

An enum is a set of logically related constants. Enum Size Small Medium Large End Enum Public Sub Order(shirtSize As Size) Select Case shirtSize Case Size.Small ' ... Case Size.Medium ' ... Case Size.Large ' ....
Each of the enum members may be initialized with a value. If a value is not specified for a member, by default it's initialized to 0 (if it's the first member in the member list) or to a value greater by 1 than the value of the preceding member. Module Module1 Enum Size Small ...
With the <Flags> attribute, the enum becomes a set of flags. This attribute enables assigning multiple values to an enum variable. The members of a flags enum should be initialized with powers of 2 (1, 2, 4, 8...). Module Module1 <Flags> Enum Material Wood = 1 ...
The HasFlag() method can be used to check if a flag is set. Module Module1 <Flags> Enum Material Wood = 1 Plastic = 2 Metal = 4 Stone = 8 End Enum Sub Main() Dim houseMaterials As Material = Material.Wood Or Material.Stone ...
An Enum instance can be created by parsing a string representation of the Enum. Module Module1 Enum Size Small Medium Large End Enum Sub Main() Dim shirtSize As Size = DirectCast([Enum].Parse(GetType(Size), "Medium"), Size) ...
Returns the names of constants in the specified Enum as a string array: Module Module1 Enum Size Small Medium Large End Enum Sub Main() Dim sizes = [Enum].GetNames(GetType(Size)) For Each size In sizes Console.WriteLine(size) Next End Sub ...
' This method is useful for iterating Enum values ' Enum Animal Dog = 1 Cat = 2 Frog = 4 End Enum Dim Animals = [Enum].GetValues(GetType(Animal)) For Each animal in Animals Console.WriteLine(animal) Next Prints: 1 2 4
The ToString method on an enum returns the string name of the enumeration. For instance: Module Module1 Enum Size Small Medium Large End Enum Sub Main() Dim shirtSize As Size = Size.Medium Dim output As String = shirtSize.ToString() ...
The next example can be used to determine whether a enumeration has the FlagsAttribute specified. The methodology used is based on Reflection. This example will give a True result: Dim enu As [Enum] = New FileAttributes() Dim hasFlags As Boolean = enu.GetType().GetCustomAttributes(GetType(FlagsAt...
In some very specific scenarios we would feel the need to perform a specific action for each flag of the source enumeration. We can write a simple Generic extension method to realize this task. <DebuggerStepThrough> <Extension> <EditorBrowsable(EditorBrowsableState.Always)> Pu...
The next example is intended to count the amount of flags in the specified flag combination. The example is provided as a extension method: <DebuggerStepThrough> <Extension> <EditorBrowsable(EditorBrowsableState.Always)> Public Function CountFlags(ByVal sender As [Enum]) As In...
The next code illustrates how to find the nearest value of a Enum. First we define this Enum that will serve to specify search criteria (search direction) Public Enum EnumFindDirection As Integer Nearest = 0 Less = 1 LessOrEqual = 2 Greater = 3 GreaterOrEqual = 4 End Enum...

Page 1 of 1