Adam
Adam

Reputation: 390

Delete a record from an array VB.NET

I'm trying to delete a record from an array in VB.Net but I can never get it to delete the correct one

In my code, intPosition is the position where the desired record I want to delete is. Customers is the name of the 3D array and NumberOfCustomers can be treated as the size of the array.

  If MsgBox("Are you sure you want to delete this record?", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
        NumberOfCustomers -= 1
        For i = intPosition + 1 To NumberOfCustomers
            Customers(i - 1) = Customers(i)
        Next
        NumberOfCustomers -= 1
        ReDim Preserve Customers(NumberOfCustomers)
        Call SaveCustomer()
    End If

Please could someone amend or find similar code for this in VB.NET.

Thanks

Upvotes: 0

Views: 72

Answers (1)

rory.ap
rory.ap

Reputation: 35260

I would propose you do without the array as it's extremely inefficient for operations like this. Instead, you should use one of the built-in classes like List(Of T) which is much better suited for how you're trying to use it.

Dim customers = New List(Of Customer)

'populate your list however you do it.

If MsgBox("Are you sure you want to delete this record?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
    customers.RemoveAt(position)
End If

Upvotes: 4

Related Questions