Here in Listing 8-1 is an example of two of the read/write properties used for
this example. The example actually contains several properties, but these
two represent standard data type and object coding. (You can find the
source code for this example on the Dummies.com site at
http://www.
dummies.com/go/vbafd5e
.)
Listing 8-1
Creating Object Properties
Private UseIcon As VbMsgBoxStyle
Private
NewIcon As Image
Public Static Property Let Icon(Value As IconTypes)
‘ Change the value of the message icon based on the
‘ input value.
Select Case Value
Case Critical
UseIcon = vbCritical
Case
Question
UseIcon = vbQuestion
Case Exclamation
UseIcon = vbExclamation
Case
Information
UseIcon = vbInformation
End Select
End
Property
Public Static Property Get Icon() As IconTypes
‘ Return the value of the message icon.
Select Case UseIcon
Case vbCritical
Icon = Critical
Case vbQuestion
Icon = Question
Case vbExclamation
Icon = Exclamation
Case vbInformation
Icon = Information
End Select
End Property
Public Static Property Set SpecialIcon(Value As Image)
‘ Set the custom icon value. Make sure the user has
‘ supplied a valid image.
If Not Value Is Nothing Then
Set NewIcon = Value
End
If End Property
Public Static Property Get SpecialIcon() As Image
‘ Return the custom icon value.
Set SpecialIcon = NewIcon
End Property
188
Part III: Expanding Your VBA Horizons
14_046500 ch08.qxp 12/5/06 5:36 PM Page 188
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: