Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 14, 1998

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 = 2
End Enum
Sub 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 If
End 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.

Charles Caison
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date