Dim iArr As Array
For Each iArr In m_alTabell
'ReDim myArr(16, 11) If i dim iArr(0,0) as string I could do this... 
ii = ii + 1
iArr(i, constanter.colNames.colValue) = m_objList.GroupItems.Item(ii).ListItems(i).Value
'm_alTabell.RemoveAt(ii - 1)
'm_alTabell.Add(iArr)
'm_alTabell.Insert(ii - 1, iArr)
'iArr.CopyTo(m_alTabell.ToArray, ii - 1)
'al.Add(iArr)
Next iArr

I have a problem with this code. m_alTabell constists of 10 twodimensional arrays. I have some data in them from the beginning. Now I want to add data in each iArr in a_alTabell. This works fine on the first one. But then the trouble starts. It just keeps overwriting the first iArr in m_alTabell. So if i could redim iArr it would solve the problem but i cant do that course it needs to be declared as Array and not "Dim iArr(0,0) as string" since that couses other problems.

Edited 5 Years Ago by __avd: Added [code] tags. Do wrap your programming code blocks within [code] ... [/code] tags.

Maybe you already tried

For Each iArr as Array In m_alTabell

This will create a 'new' iArr at each element in m_alTabell

Hope this helps

Hi, no I hadn't tried that but it doesn't work unfortunatly... This overwrights iArr everty time also.

Please, can you post an example of the contents of the arrays and the final foreach you had written to try to reproduce the problem?

For Each iArr As Array In m_alTabell
            ii = ii + 1
            For i = 0 To 16
                If m_objInputList.ListItems(i).RowType = RowType.Edit Or m_objInputList.ListItems(i).RowType = RowType.ListEdit Or m_objInputList.ListItems(i).RowType = RowType.DialogButtonEdit Then
                    iArr(i, constanter.colNames.colValue) = m_objInputList.GroupItems.Item(ii).ListItems(i).Value
                    If i = 0 Then
                        If m_objInputList.GroupItems.Item(ii).ListItems(i).Value = "" Then
                            bAntalSaknas = True
                        End If
                    End If
                    If m_objInputList.GroupItems.Item(ii).ListItems(i).Value = "Makulerad" Then
                        bEmpty = True
                        nListIndex = nListIndex + (nSubItems - nRow)
                        Exit For
                    End If
                    If m_objInputList.GroupItems.Item(ii).ListItems(i).Value <> "" And m_objInputList.GroupItems.Item(ii).ListItems(i).Value <> "N" And m_objInputList.GroupItems.Item(ii).ListItems(i).Value <> "n" Then
                        bEmpty = False
                    End If
                End If
            Next i
'Maybe I'm not using the right way to add the array to the arraylist???
            'm_alTabell.RemoveAt(ii - 1)
            'm_alTabell.Add(iArr)
            'm_alTabell.Insert(ii - 1, iArr)
            'iArr.CopyTo(m_alTabell.ToArray, ii - 1)
            'al.Add(iArr)
            alArray.Add(iArr)
        Next

Edited 5 Years Ago by __avd: Added [code] tags.

This works fine:

Dim a(3) As String
        Dim i As Integer
        Dim al As New ArrayList

        For i = 0 To 3
            a(0) = "l"
            a(1) = "2"
            a(2) = "3"
            al.Add(a)
        Next i

        For Each Array In al
            a(0) = "2"
        Next

But when I try to to for each Array in my other application I get an error stating that Array is a type and can not be used as an expression.

Dim a(2, 2) As String
        Dim b(2, 2) As String
        Dim i As Integer
        Dim al As New ArrayList

        For i = 0 To 2
            a(i, 0) = "l"
            a(i, 1) = "1"
            a(i, 2) = "1"
            al.Add(a)
        Next i
        i = 0
        For Each Array In al
            al(i)(i, 0) = "7"
            i = i + 1
        Next

This doesnt work eather. When this has looped once all arrays has changed at position i,0 to "7"

hi try this link to get clear idea about the for each loop.. [blog spam removed]

Edited 5 Years Ago by Ezzaral: n/a

Hi, no sorry, that doesnt work since I have values in the different positions in the string array that I want to keep. This is what I did and this works but it's really ugly.
For every array in my collection I do this. Instead of beeing able to just copy
the specific position in the collection to my specific array i have to:
Make an new array for each one and loop through the specific array in the collection and actually add all the values to the new array.

For o As Integer = 0 To 16
            For a As Integer = 0 To 11
                aNollArray(o, a) = m_alTabell.Item(1)(o, a)
                If a = 11 Then Exit For
            Next a
        Next o
alArray.Add(aNollArray)

Why this works and the other way don't, I don't know but I would like to find out.

If I do this:

aNollArray=m_alTabell.Item(1)

What happens when I loop through it after assigning it this way it changes all the arrays in the collection, not just the one I tell it to change.

Just because your array being added to the arraylist is the same for each instance.

If you do not create a new array inside the for each loop, the array memory address is always the same (never changed), so, even added several times to the array list, internally the array list only contains the addres of the unique array being added (n times). A change to any of the items in the array list will change all.

You need to create a new array to hold the data, then fill it, then add to the array list, and start over for the next instance.

Hope this helps

This article has been dead for over six months. Start a new discussion instead.