vb net append list of with list of item/index

618 Views Asked by At

I have a custom control object called cc1. I store cc1 like this Public cc1List As New List(Of cc1) and add stuff to it, works like a charm.

Now i want to remove some item inside it, with a known index, so i create Dim cc1ListTemp As New List(Of cc1), loop through the whole index of cc1, and append all items excluding the removed index through this Code: cc1ListTemp.Add(cc1List(i)) but it keeps on throwing an OutOfRangeException, beginning with the first loop (i = 0) all the way up.

But i know for sure that cc1List(i) where i = 0 to 5 is fully populated, and checked even in runtime before triggering the exception (i can fully access/edit/call cc1List(0)).

Do i need to append in a different way? If needed i can provide more code.

Edit0:

        Dim cc1ListTemp As New List(Of cc1)
        For i = 0 To CWBListMaxIndex
            If Not i = IndexToRemove Then
                cc1ListTemp.Add(cc1List(i))
            End If
        Next
        cc1List = cc1ListTemp

Adding works fine, mixed add/append.

cc1List.Append(cc1TempObject)
cc1List.Add(cc1TempObject)

Edit1: reproduced it with Textboxes replacing cc1 inside a new project.

1

There are 1 best solutions below

0
Mary On

Since you know what index you want to remove, use the RemoveAt method.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lst As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3, TextBox4}
    Dim IndexToRemove = 1
    lst.RemoveAt(IndexToRemove)
    For Each TB In lst
        Debug.Print(TB.Name)
    Next
End Sub

Prints

TextBox1
TextBox3
TextBox4

in the Immediate Window