LibreOffice 7.3 laguntza
Bildumak mota askotako elementuak biltegiratzeko erabili daitezke. Elementu bakoitza bere indizearen bidez edo aukerako gako baten bidez atzitu daiteke.
Collection objektuek honako metodoak dituzte:
Add: Elementu berria txertatzen du bilduman. Aukeran, kate-balio bat definitu daiteke elementuaren gako gisa.
Count: Bildumako elementu kopurua itzultzen du.
Item: Bildumako elementu bat itzultzen du, bere indizea edo gakoa pasatuta.
Remove: Elementu bat bildumatik kentzen du, bere indizearen edo gakoaren bidez.
Bilduma bateko elementuak atzitzeko, bere indizeak (dimentsio bakarreko matrize gisa) edo lotutako gakoak erabili daitezke.
The ScriptForge Dictionary service extends the Collection object by providing supplemental features as key retrieval and replacement, as well as import/export to Array objects and JSON strings.
Collection bat sortzeko, erabili New gako-hitza. Hurrengo adibideak Collection objektu bat sortzen du eta hiru elementurekin betetzen du:
Dim myCollection as New Collection
myCollection.Add("Some text")
myCollection.Add(100)
myCollection.Add(Array(1, 2, 3, 4))
MsgBox myCollection.Count ' 3
Add metodoak elementu berriak gehitzen dizkio Collection objektuari.
oCollection.Add(item, [key], [before|after])
item: Collection bildumari gehituko zaion elementua. Edozein motatakoa izan daiteke.
key: Balio hau identifikatzeko erabiliko den gako bakarrra den kate-balioa.
before, after: Aukerako gako-hitza, elementu berria Collection objektuko zein tokitan kokatuko den adierazten duena. Bi argumentuetako bakarra, before edo after zehaztu daiteke aldi berean. Elementu berria zein indizeren edo gakoren aurretik (edo ondoren) kokatuko den zehazten du.
Beheko adibidean, bi elementu gehitzen zaizkio Collection bati. Lehenak gako bat du lotuta, eta bigarrenak ez du.
Dim myCollection as New Collection
myCollection.Add(100, "first")
myCollection.Add(101)
Add metodoak gako-hitzak diren argumentuak ere onartzen ditu:
myCollection.Add(item := 100, key := "first")
Keys must be unique in a Collection object. Comparison between keys is case-insensitive. Adding duplicated keys will result in a runtime error.
The example below illustrates how to use the Before and After keyword arguments to determine the position of the item that is being added.
Dim myCollection as Variant
myCollection = New Collection
myCollection.Add(item := 101, key := "first")
myCollection.Add(item := 103, key := "third")
myCollection.Add(item := 105, key := "fifth")
MsgBox myCollection.Item(2) ' 103
myCollection.Add(item := 102, key := "second", before := "third")
MsgBox myCollection.Item(2) ' 102
myCollection.Add(item := 104, key := "fourth", after := 3)
MsgBox myCollection.Item(4) ' 104
Items in a Collection object are assigned an integer index value that starts at 1 and corresponds to the order in which they were added.
Use the Item method to access a given item by its index or key.
oCollection.Item(index)
oCollection.Item(key)
index: an integer value specifying the index of the item to be returned.
key: a string value specifying the key of the item to be returned.
Dim myCollection as New Collection
myCollection.Add(item := 101, key := "A")
myCollection.Add(item := 102, key := "B")
myCollection.Add(item := 103, key := "C")
MsgBox myCollection.Item("A") ' 101
MsgBox myCollection.Item(3) ' 103
Use the Remove method to delete items from a Collection object.
Items can be removed either by their indices or key values.
oCollection.Remove(index)
oCollection.Remove(key)
index: an integer value specifying the index of the item to be removed.
key: a string value specifying the key of the item to be removed.
Dim myCollection as New Collection
myCollection.Add(item := 101, key := "first")
myCollection.Add(item := 102, key := "second")
myCollection.Add(item := 103, key := "third")
MsgBox myCollection.Count ' 3
' Removes the first value
myCollection.Remove(1)
' Removes the value whose key is "third"
myCollection.Remove("third")
MsgBox myCollection.Count ' 1
It is possible to use a For Each ... Next statement to iterate over all items in a Collection.
Dim myCollection as New Collection
myCollection.Add(item := 101, key := "A")
myCollection.Add(item := 102, key := "B")
myCollection.Add(item := 103, key := "C")
For Each value In myCollection
MsgBox value
Next value
To remove all items from a Collection object call the Remove method for each item, as illustrated in the example below:
' Create a sample Collection with two entries
Dim myCollection as New Collection
myCollection.Add(item := 10, key := "A")
myCollection.Add(item := 11, key := "B")
MsgBox myCollection.Count ' 2
' Removes all items in the collection
For i = myCollection.Count To 1 Step -1
myCollection.Remove(i)
Next i
MsgBox myCollection.Count ' 0