Word Document Properties


Chapter 9: Working with Arrays and Collections



Download 8,82 Mb.
Pdf ko'rish
bet186/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   182   183   184   185   186   187   188   189   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Chapter 9: Working with Arrays and Collections
15_046500 ch09.qxp 12/5/06 5:36 PM Page 223


Listing 9-7
(continued)
‘ Add an entry with a Key and Before value.
PersonCollection.Add Item, Key, Before
‘ Determine whether there is an After value.
ElseIf After > 0 Then
‘ Add an entry with a Key and After value.
PersonCollection.Add Item, Key, , After
Else
‘ The entry is just an Item and a Key.
PersonCollection.Add Item, Key
End If
Else
‘ Determine whether there is a Before value.
If Before > 0 Then
‘ Add an entry with a Before value.
PersonCollection.Add Item, , Before
‘ Determine whether there is an After value.
ElseIf After > 0 Then
‘ Add an entry with an After value.
PersonCollection.Add Item, , , After
Else
‘ The entry is just an Item.
PersonCollection.Add Item
End If
End If
End Sub
Public Property Get Count() As Long
‘ Return the current collection count.
Count = PersonCollection.Count
End Property
Public Sub Remove(Index As Variant)
‘ Remove the requested item.
PersonCollection.Remove Index
End Sub
Public Property Get Item(Index As Variant) As Person
‘ Return the requested item.
Set Item = PersonCollection.Item(Index)
224
Part III: Expanding Your VBA Horizons 
15_046500 ch09.qxp 12/5/06 5:36 PM Page 224


End Property
Private Sub Class_Initialize()
‘ Initialize the collection.
Set PersonCollection = New Collection
End Sub
Notice that the code begins by creating a 
Collection
object. You could also
use the 
Implements
statement to implement the 
Collection
class, but 
this technique is easier. The 
Class_Initialize
method initializes
PersonCollection
so that other methods can use it.
The 
Add
method is the most complex sub-procedure that you write for a col-
lection in most cases. The reason for the complexity is that this method has
so many optional arguments. In addition, when you supply a 
Before
argu-
ment, you can’t supply an 
After
argument — the two are mutually exclusive.
The code divides the task of determining what to do into a 
Key
or no 
Key
decision. It then decides whether you supplied a 
Before
or 
After
argument,
or neither, and takes the appropriate action. Look through the 
Add
method
code, and you see that each decision results in a different 
Add
method-calling
syntax.
The 
Count
property returns the 
PersonCollection.Count
property. You
never need to add error-handling code to this property because it’s read only.
Never make this property read/write. You don’t want someone using the col-
lection to change the count.
The 
Remove
method makes a direct call to the 
PersonCollection.Remove
method. You could add range checking to this method by using the same
techniques that I use in previous examples. The 
PersonCollection.Remove
method raises an error when you supply an incorrect value. Notice the use of

Variant
for this method so that it can accept a string or an integer as input.
The 
Item
property is also read only. Again, you should never make this prop-
erty read/write. Always use the 
Add
method to add new entries to the collec-
tion. Notice that this method returns an object, so you have to use the 
Set
statement.
Creating a default property
No matter which collection you look at, 
Item
is the default property or func-
tion. Figure 9-3 shows that 
Item
uses a special symbol. The explanation for
Item
in the lower pane of the Object Browser window also says that this is
the default property of the 
Fields
collection. The only problem is that VBA
doesn’t provide a direct method for you to create a default property or
method for your class.
225

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   182   183   184   185   186   187   188   189   ...   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