Define Enumerations
Enumerations are a handy way to publish the possible settings for a property. For example, the following addition to the Message class allows users to set the icon that appears on the message when it is shown:
Public Icon As IconType
Enum IconType
None
Critical = VbMsgBoxStyle.vbCritical
Warning = VbMsgBoxStyle.vbExclamation
Question = VbMsgBoxStyle.vbQuestion
Information = VbMsgBoxStyle.vbInformation
End Enum
' Show method: displays the message.
Public Sub Show( )
MsgBox value, Me.Icon, Title
End SubI added Me.Icon to the Show method to display the appropriate icon in the MsgBox. The point of using an enumeration is that the available settings are now automatically listed when you set the property, as shown in Figure 5-4.
You can use enumerations
within methods as well. For example, the following changes allow the Show method to accept an icon setting:
Public Sub Show(Optional icon As IconType = -1)
If (icon = -1) Then icon = Me.icon
MsgBox value, icon, Title
End SubIn the preceding code, I made icon an optional argument with a default setting outside of the possible IconType values so I can tell whether or not the argument was set. If icon is omitted, I use the setting from the Icon property instead. In this case, the icon argument overrides the Icon property.
When you use Show, Visual Basic displays the possible settings for the icon argument, as shown in Figure 5-5.
Figure 5-4. Use enumerations to publish available settings
Figure 5-5. Enumerations are handy ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access