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# Then		RightNow = Morning	ElseIf Time >= #12:00:00 PM# And Time < #6:00:00 PM# Then		RightNow = Afternoon	ElseIf 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist