Use of collection object in macro basic

LO macros support collection objects. They are versatile objects, but very little documentation is found on the internet. Why ?
An alternative is to use subroutines in Python or java, or javascript. But who wants to stay in Basic? Why so little online support?
This test code describes some basic operation

 Dim t As New Collection
  dim a(2) as integer
  a(0)=10:a(1)=20: tc=thisComponent
  t.Add("fx", "x")  ' adding string with key "x"
  t.Add("fy", "y") 
  t.Add("fz", "z")  
  t.Add(a, "k")       
 
  s1=t("k")(0)        ' extract array a from item "k" of test, read element 0
  t.Add(tc, "tc")
  s2=t("tc").wordCount
  vc=tc.currentController.viewCursor
  vc.gotoStart(false)
  vc.goDown(5,true)
  t.Add(tc.currentSelection, "cs1")
  vc.goDown(2,false)
  vc.goDown(5,true)
  t.Add(tc.currentSelection, "cs2")  
 msgbox(test("cs1").getByIndex(0).string)
 msgbox(test("cs2").getByIndex(0).string)
     ' scan collection, two methods
  For i = 1 To t.Count
    '  o=test(i)
   Next  
     s=""
   For Each m In t
      ' m is value of current item, it's not the key of item 
   Next
   t = nothing

It’s possible to obtain list keys as in javascript Object.keys(t) ?

Indeed, the Collection object is not documented. However I recommend you have a look at ScriptForge.Dictionary service that’s extending Basic Collection object. The Dictionary service is available from LibreOffice 7.1 onwards.

Thanks so much. The XEnumerableMap interface Interface XEnumerableMap was already available in previous releases of LO which offered a similar service.

On the net, even if with difficulty, there is sufficient documentation on the collection object which largely reproduces the equivalent available in visualbasic for application

Basic collection object documentation in help is now available
see Collection Object

1 Like