The first property,
Icon
, uses a standard data type. In this case, it’s an enu-
merated data type that ensures that you provide the correct values. See the
“Using enumerated constants” section, later in this chapter, for details on
using enumerated types. Notice that the code transfers the input value to
the private
UseIcon
variable only after it checks the input for correctness.
When you work with a non-enumerated data type, it pays to include an
Else
Case
clause that displays a message with correct input values. Using an
enumerated type means that you don’t have to include this feature.
Notice that
UseIcon
is a variable that is based on the
VbMsgBoxStyle
enu-
meration. An enumeration is a special kind of data structure that contains
special values — it’s based on the
enum
data type, so the
UseIcon
variable
lets you select one of the enumeration values. Using an enumeration simply
makes the code easier to read.
The second property,
SpecialIcon
, requires an object as input. This means
that you must use the
Set
and
Get
methods rather than the
Let
and
Get
methods that the first property uses. Data-type checking is less intense in this
case because VBA always provides a type mismatch error message if you pro-
vide the wrong value.
NewIcon
is an object based on the
Image
class. The
Image
class describes
how to build a container for holding an image, such as a bitmap. The
NewIcon
object actually holds the image that you provide as input to the
SpecialIcon
property.
Notice that you still have to check for empty objects that don’t contain any-
thing. The code shows how to perform this task by using the
Is Nothing
keyword sequence. When you require specific kinds of input for objects, you
need to check object property values as well. This example doesn’t perform
this task, but it’s something that you should consider for complex properties.
For example, an image should have a valid
Picture
property value.
Property conversion considerations
When you choose to encapsulate a function to make it easier to use, you
can run into situations where there isn’t a direct conversion between
the function and the object version. The
MsgBox
function includes the
vbMsgBoxHelpButton
style. This feature works better as a
Boolean
property, so you use the following code to create a property for it:
Public Static Property Let HelpButton(Value As Boolean)
‘ Should the example use the vbMsgBoxHelpButton style?
UseHelpButton = Value
End Property
Public Static Property Get HelpButton() As Boolean
‘ Return the vbMsgBoxHelpButton value.
HelpButton = UseHelpButton
End Property
189
Do'stlaringiz bilan baham: