Gambas Documentation - /comp/gb/collection

/comp/gb/collection

# Collection (gb)

Implements an hash table whose keys are String and values are Variant.

`NULL` is used when nothing is associated with a given key. Consequently, associating `NULL` with a key is the same thing as removing it from the collection.

The size of the internal hash table grows dynamically as data is inserted.

This class is
Creates a new collection.
creatable
.
This class acts like a
Returns the value associated with Key.
read
/
Associates a value with Key.
write
array.
This class is
Enumerates each value stored in the collection.
enumerable
with the FOR EACH keyword.

Properties | Methods
--- | ---
Returns the number of elements stored in the collection. Count Returns the key of the last read or last enumerated element. Key Returns the number of elements stored in the collection. Length | Adds an element to the collection. Add Clears the collection. Clear Returns a copy of the collection. Copy Returns if something is associated with this key in the collection. Exist Removes an element from the collection. Remove

### Example

    PUBLIC SUB Main()
      DIM cAbbr2Weekday AS Collection = ["mo": "Monday", "tu": "Tuesday", "we": "Wednesday", "th": "Thursday", "fr": "Friday", "sa": "Saturday", "su": "Sunday"]
      DIM cCopy AS Collection
      DIM sAbbr AS String

      ' To access the data
      PRINT cAbbr2Weekday["fr"]

      ' Print mapping key to value
      FOR EACH sAbbr IN cAbbr2Weekday
        PRINT cAbbr2Weekday.Key & " --> " & sAbbr
      NEXT

      ' Lenght/Count return both 7
      PRINT cAbbr2Weekday.Count
      PRINT cAbbr2Weekday.Length

      ' Add element and remove it again
      cAbbr2Weekday.Add("sat", "Saturday")
      cAbbr2Weekday.Remove("sat")

      ' Test if an key is present
      PRINT cAbbr2Weekday.Exist("we")

      ' Make a copy and clear the original collection
      cCopy = cAbbr2Weekday.Copy()
      cAbbr2Weekday.Clear()

    END