Word Document Properties


Chapter 8: Object-Oriented Programming



Download 8,82 Mb.
Pdf ko'rish
bet158/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   154   155   156   157   158   159   160   161   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Chapter 8: Object-Oriented Programming
14_046500 ch08.qxp 12/5/06 5:36 PM Page 187


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

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   154   155   156   157   158   159   160   161   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©www.hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish