devxlogo

Enums Improve Readability

Enums Improve Readability

An enumerated type is simply a list of constants, each of which has a unique value. An Enum can improve the readability of your code by allowing you to declare a variable of your enumerated type, then assign one of the elements of the Enum to the value of the variable. Visual Basic makes this easy by providing you with a list of values for your variable as you use it:

 Public Enum TimeOfDay	Morning = 0	Afternoon = 1	Evening = 2End EnumSub Main()	Dim RightNow As TimeOfDay	If Time >= #12:00:00 AM# And Time = #12:00:00 PM# And Time = #6:00:00 PM# Then		RightNow = Evening	End IfEnd Sub

Validity checking is not done when assigning values to the special-typed variable. Even though the TimeOfDay values range from zero to two, VB doesn’t attempt to keep you from assigning a value of four, or another number, to such a variable. Be especially aware of this when writing Property Let procedures, which accept an enumerated parameter.

devx-admin

Share the Post: