0

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

7
  • 3
    You are torturing the array. Use a List(Of T) or something better suited to the types of things you are trying to do. Commented Jan 12, 2016 at 17:56
  • 1
    rather than an array, if it was a List(Of Customer) (which will take all of 15 mins to learn about) Customers.RemoveAt(indexToRemove) is all you need Commented Jan 12, 2016 at 17:58
  • Also, are you sure this is VB.NET? There is no such enum MessageBoxButtons. It should be MsgBoxStyle. Commented Jan 12, 2016 at 17:59
  • Maybe they created their own message box? Commented Jan 12, 2016 at 18:00
  • @roryap There's a MessageBoxButtons enum in the System.Windows namespace, although it should be used with the MessageBox class and not with the MsgBox VB function. Commented Jan 12, 2016 at 18:39

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.