Logo Foltyn Presentation
Table of Contents Previous Page Next Page
Content-Symbol-Img

Ein Programm-Beispiel in der Programmiersprache A program-example in the programming language
Visual BASIC 2015



Das Programm ist getestet vor der Publikation, aber es kann keine Garantie gegeben werden, dass es fehlerfrei ist

The program ist tested before publication, but there can be given no guarantee, that it is free of errors
30. Sept. 2016 Sept 30th 2016


Tutorial for Use of ArrayList- and Collection-Objects

We are talking here always about dynamic arrays, which can hold a variable number of items, because in data processing all data are worked down linewise and processed by adding and deleting any number of lines by the program. Arrays with fixed dimensions rarely occur.

So it can occur to have the need to use an Array, which can hold a list of different Arrays with different number of items. For exmaple:

        Dim MyArray1 As String() = {"Msg11"}
        Dim MyArray2 As String() = {"Msg21", "Msg22", "Msg23"}
        Dim MyArray3 As String() = {"Msg31", "Msg32"}
        Dim MyArray4 As String() = {"Msg41", "Msg42", "Msg43", "Msg44"}
        Dim AL As New ArrayList
        AL.Add(MyArray1)
        AL.Add(MyArray2)
        AL.Add(MyArray3)
        AL.Add(MyArray4)

This way has the advantage, that you need not convert the arrays to strings, for which you must occupy a separator. In this method there is no separator.

So we have to premention the following:

In Visual BASIC 2015 a List(Of String) is an object, which offers a serie of possibilities as there are:

        Dim LofS As New List(Of String)
        LofS.Add("Msg1")
        LofS.AddRange({"Msg2, "Msg3", "Msg4", "Msg5", "Msg6"})
        LofS.Insert(StartPos, "Msg7") ' StartPos zerobased
        LofS.InsertRange(StartPos, {"Msg8", "Msg9"})
        LofS.RemoveAt(Pos) ' Pos zerobased
        LofS.RemoveRange(StartPos, 3)

Which the object MyList As String() does not have,
Conversion from LofS to MyList can be done by

         MyString = LofS.ToSTring or by MyArray = LofS.ToArray

But conversion from String() to LofS works only like this:

        Dim MyList As String() = {"Msg1", "Msg2", "Msg3"}
        Dim LofS As New List(Of String) : LofS.AddRange(MyList)

Now we have two possibilities to store List(Of String)s in an Array and get them out again.

1. With an ArrayList
The ArrayList cannot hold LofS-objects as items, they must be converted ToArray() to load them into the ArrayList and for getting them out they must be reconverted into LofS as shown above.
2. With an ObjectCollection
The ObjectCollection can hold LofS-objects, but they must be decoupled from assignment to load them into the collection. They can be got out as LofS.

1. ArrayList

    Private Sub TestOfArrayList()
        Dim AL As New ArrayList, LS, Elmts, AllTxt As New List(Of String), SL As String()
        ' Loading ArrayList with values
        LS.Clear() : LS.AddRange({"txt11"}) : AL.Add(LS.ToArray)
        LS.Clear() : LS.AddRange({"txt21", "txt22", "txt23"}) : AL.Add(LS.ToArray)
        LS.Clear() : LS.AddRange({"txt31", "txt32"}) : AL.Add(LS.ToArray)
        ' Getting Values out
        For Each SL In AL : LS.Clear() : LS.AddRange(SL) : Elmts.Clear()
            For Each item In LS : Elmts.Add(item) : Next
            AllTxt.Add(String.Join(",", Elmts))
        Next : TextBox1.Text = String.Join(vbCrLf, AllTxt)
    End Sub

2. ObjectCollection

If you write
        Dim obj1, obj2 As Object
        obj1 = {"Msg1", "Msg2"}
        obj2 = obj1
        obj1 = "something else"

then in obj2 now there is "something else", because it is referenced.

So you cannot write:

        Dim ObjColl As New Microsoft.VisualBasic.Collection()
        Dim LofS As New List(Of String)
        LofS.Clear() : LofS.AddRange({"Msg1"}) : ObjColl.Add(LofS)
        LofS.Clear() : LofS.AddRange({"Msg2"}) : ObjColl.Add(LofS)
        LofS.Clear() : LofS.AddRange({"Msg3"}) : ObjColl.Add(LofS)

Without getting in each line of ObjColl the same content, namely "Msg3" and if you change now LofS in all items will be the changed.

So this cannot be used to load an object-collection by program-collected data and LofSs must be decoupled either by a function like AddLofS or CLofS, which is always in my programs as standard for general use.

    Private Sub TestOfObjectCollection()
        Dim ObjColl As New Microsoft.VisualBasic.Collection()
        Dim LS, A, LofS As New List(Of String)
        ' Loading ObjColl with values
        ObjColl.Add(AddLofS({"txt11"})) ' AddLofS to decouple assignment
        ObjColl.Add(AddLofS({"txt21", "txt22", "txt23"}))
        ObjColl.Add(AddLofS({"txt31", "txt32"}))

        ObjColl.Add(CLofS({"txt11"})) ' CLofS to decouple assignment
        ObjColl.Add(CLofS({"txt21", "txt22", "txt23"}))
        ObjColl.Add(CLofS({"txt31", "txt32"}))
        ' Getting Values out
        For Each LofS In ObjColl
            If TypeOf LofS Is List(Of String) Then MsgBox("it is")
            A.Add(String.Join(", ", LofS))
        Next : TextBox1.Text = String.Join(vbCrLf, A)
    End Sub

    Private Function AddLofS(ByVal xList As String()) As List(Of String)
        Dim LofS As New List(Of String) : LofS.AddRange(xList) : Return LofS
    End Function

    Public Function CLofS(ByVal xVar As Object) As List(Of String)
       ' Converts Strings and LofS in LofS like my earlier PUSH did
      Dim A As New List(Of String)
        If TypeOf xVar Is List(Of String) Then A.AddRange(xVar)
        If TypeOf xVar Is String() Then A.AddRange(xVar)
        If TypeOf xVar Is String Then A.Add(xVar)
        If TypeOf xVar Is Integer Then A.Add(CStr(xVar))
        Return A
    End Function