Collection objektua

Bildumak mota askotako elementuak biltegiratzeko erabili daitezke. Elementu bakoitza bere indizearen bidez edo aukerako gako baten bidez atzitu daiteke.

Collection objektuek honako metodoak dituzte:

note

Bilduma bateko elementuak atzitzeko, bere indizeak (dimentsio bakarreko matrize gisa) edo lotutako gakoak erabili daitezke.


tip

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.


Bilduma bat sortzea

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
  

Elementuak gehitzea

Add metodoak elementu berriak gehitzen dizkio Collection objektuari.

Sintaxia:

oCollection.Add(item, [key], [before|after])

Parametroak:

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.

Adibidea:

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")
  
warning

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
  
note

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.


Elementuak atzitzea

Use the Item method to access a given item by its index or key.

oCollection.Item(index)

oCollection.Item(key)

Parametroak:

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.

Adibidea:


    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
  

Elementuak kentzea

Use the Remove method to delete items from a Collection object.

Sintaxia:

Items can be removed either by their indices or key values.

oCollection.Remove(index)

oCollection.Remove(key)

Parametroak:

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.

Adibidea:


    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
  

Iterating Over all Items

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
  

Clearing a Collection

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