Storing Arraylist in System.Collections.ArrayList

Storing Arraylist in System.Collections.ArrayList

mucip
Collaborator Collaborator
1,325 Views
3 Replies
Message 1 of 4

Storing Arraylist in System.Collections.ArrayList

mucip
Collaborator
Collaborator

Hi,

I use System.Collections.ArrayList in my VBA code. I want to add one array to this Array like below:

 

Dim lateralListesi As Object
Dim i as integer
Set lateralListesi = CreateObject("System.Collections.ArrayList")
lateralListesi.add Array(pipeType, pipeLength)

 

 

After than add many pipeTypes I want to check pipeTypes in the lateralListesi. If I added same type in the lateralListesi before than I want to increase pipeLength only like pipeLength = pipeLength + newlyPipeLength

'I read pipeType before. and check if I added before?
For i = 0 To lateralListesi.Count - 1
            If lateralListesi.Item(i)(0) = pipeType Then
                'Early added.
                lateralListesi.Item(i) = Array(pipeType , Val(lateralListesi.Item(i)(1)) + newlyPipeLength)                
            Else
                'Just new adding.
                lateralListesi.add Array(pipeType, newlyPipeLength)
            End If
        Next i

 

But I don't know why I cannot check inner array like ;

lateralListesi.Item(i)(0)

or

lateralListesi (i) (0)

 

Where am I wrong? I need two column temp table infact. I did not want to use dim and reDim. Because I don't know the row count in the begining.

 

Regards,

Mucip:)

 

 

 

 

 

 

0 Likes
1,326 Views
3 Replies
Replies (3)
Message 2 of 4

norman.yuan
Mentor
Mentor

Are you sure it is the line of code that does not work:

 

lateralListesi.Item(i)(0)

 

The following code sample works ok with my AutoCAD2020/2021 VBA, whether I use ArrayList(index), or ArrayList.Item(index):

Option Explicit

Public Sub Test()

    Dim arr As Object
    Set arr = CreateObject("System.Collections.ArrayList")
    arr.Add Array(1.11, 2.22)
    arr.Add Array(10.11, 20.22)

    Dim i As Integer
    Dim num1 As Double
    Dim num2 As Double
    For i = 0 To arr.Count - 1
        num1 = arr.item(i)(0)
        num2 = arr.item(i)(1)
        
        '' == following also works
        '' num1 = arr.item(i)(0)
        '' num2 = arr.item(i)(1)
        
        MsgBox "num1=" & num1 & vbCrLf & "num2=" & num2
    Next

End Sub

 

So, as long as you have legitimate ArrayList and enabled your AutoCAD that uses .NET framework 4.x to use legacy .NET 2.x runtime activation policy. That is, if you have not enable this, this line would raise error:

 

CreateObject("System.Collections.ArrayList")

 

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 4

mucip
Collaborator
Collaborator

Dear @norman.yuan ,

 

 

arr.item(i)(0)= 25

 

 

I could not assign like this according to your sample:

 

 

 

arr.item(i) = array(25,15)

 

 

 

And I tyed below code and it worked I don't know why?

 

 

Regards,

Mucip:)

 

 

0 Likes
Message 4 of 4

mucip
Collaborator
Collaborator

Hi,

Any feedback about this problem?

 

Regards,

Mucip:)

0 Likes