How to create array of lists in LotusScript

12,956

Solution 1

If you explicitely declare a variable as Array (as you do in your Redim Statement), then it can not be "reassigned" using arrayappend.

And it is NOT necessary to do it that way. just replace the line MyACL = ArrayAppend(MyACL,Person) with MyACL(Ubound(MyACL)) = Person

Take care: With that example code you will never fill MyACL(0) as the first Element filled is MyACL(1)

To begin filling the array with element 0 the code needs to be changed like this:

Dim max As Integer
max = 0
ForAll thisName In names
    ReDim Preserve MyAcl(max)
    Person("Name") = thisName
    MyACL(max) = Person
    max = max + 1
End ForAll

BUT: I don't know, if this is a good idea, as you can not access the "Detail1- Property" of Person directly.

Something like

detail = MyACL(1)("Detail1")

is not possible. You always have to have a temporary variable like this:

person = MyACL(1)
detail = person("Detail1")

Solution 2

This is a typical example of when you want to use a class instead, and create an array of that class. That class, in turn can contain a list (as well as other things). Can be very powerful!

Updated:

The benefit of using a class is that you can add business logic in the class, and it is very easy to extend it with more functionality later. Below is an example, based on the question above, but with additional functionality.

Class PersonObject
    Public PersonAttribute List As String
    Public NABdoc As NotesDocument
    Public PersonName As String

    Public Sub New(personname As String)
        Dim nab as New NotesDatabase("Server/Domain","names.nsf")
        Dim view as NotesView
        '*** Get person document from Domino directory
        Set view = nab.GetView("PeopleByFirstName")
        Set me.NABdoc = view.GetDocumentByKey(personname)
        '*** Set person name in object
        me.PersonName = personname
        '*** Set some values from person doc
        me.PersonAttribute("Email") = GetValue("InternetAddress")
        me.PersonAttribute("Phone") = GetValue("OfficePhone")
    End Sub

    Public Function GetValue(fieldname as String) as String
        GetValue = me.NABdoc.GetItemValue(fieldname)(0)
    End Function

    Public Sub AddAttribute(attributename as String, value as string)
        me.PersonAttribute(attributename) = value
    End Sub

End Class

You can now very easily build you a list, using this class (and assuming that names is a list of unique names):

Dim person List As PersonObject
Dim personname As String

ForAll n in names
    '*** Create person object based on name
    person(n) = New PersonObject(n)
    '*** Store additional info about this person
    person.AddAttribute("Age","35")
End ForAll    

Hopefully this gives you an idea of what you can do with classes.

You can also take a look at the following two blog entries about the basics of object oriented Lotusscript:

http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-1/

http://blog.texasswede.com/object-oriented-lotusscript-for-beginners-part-2/

Share:
12,956
JakubM
Author by

JakubM

Updated on June 26, 2022

Comments

  • JakubM
    JakubM almost 2 years

    I have code like this:

        Dim MyACL As Variant
        Dim Person As List
    
        Redim MyACL(0)
    
        Person("Detail1") = "Something1"
        .
        .
        .
        Person(Detailx") = "Somethingx"
    
        ForAll name in names
           ReDim Preserve MyAcl(Ubound(MyACL)+1)
           Person("Name") = name
           MyACL = ArrayAppend(MyACL,Person)
       End ForAll
    

    It throws error "Type Mismatch". Do you know, how to create an array of lists? Thank you.