Enumerations
An enumeration is a set of named integer constants.
Enumerations are value types
Enumerations are especially suitable for representing types that can take one of a set of fixed values.
They must always be fully qualified with the enumeration type name.
The default type is "int" although the type can also be (byte, sbyte, short, ushort, uint, long, ulong)
public enum EN_SHAPE // defaults to int
{
Triangle,
Square,
Rectangle,
Circle,
Unknown,
[Stop] //Stop is a reserved word
}
EN_SHAPE Shape = EN_SHAPE.Triangle;
byte Data Type
public enum EN_SHAPE : byte
{
SomeThing
}
GetName Method
This retrieves the name of the constant instead of the numerical value
System.Windows.Forms.MessageBox.Show(System.Enum.GetName(typeof(EN_SHAPES,2); // = Green
If numerical values are not specified then
1 plus the previous constant is used
0 is used for the first constant
Enumerations are value types and they differ from the abstract System::Enum class which in turn derives from System::ValueType
Any Enum you define in your application derives from the System.Enum, which in turn inherits from System.ValueType.
If no explicit value is provided for the first member inside the Enum block is assigned the value 0, the second is assigned 1, etc
You are encouraged not to change the initial value.
Enum blocks can appear anywhere in a source file, an inside module, a class or a structure block or directly at the namespace level
VB.Net
Enum EN_SHAPES 'As integer - by default
Triangle ' This is assumed to be zero
Square '1
Rectangle '2
Circle ' 3
Unknown = -999
[Stop] 'Stop is a reserved word
End Enum
Dim Shape As EN_SHAPES = EN_SHAPES.Triangle
System.Windows.Forms.MessageBox.Show([System.Enum].GetName(GetType(EN_SHAPES,2); // = Green
Even though Enum values are internally stored as integers, by default you are not allowed to assign a number to an Enum variable if Option Strict is on and you must convert the number to the proper Enum type before assigning it an Enum variable
Shape = CType(1, enShape)
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext