Domanda

I've got the following:

For Each curCustomer As Customer In _customersEdit
    If (IsNothing(curCustomer)) Then
        Continue For
    End If
    'other code
    If (curCustomer.SeatIDs(0) = primarySeat) Then
        'other code
    End If

    If (curCustomer.SeatIDs.Count = 0) Then
        curCustomer = Nothing
    End If
Next

After running this code once it's fine, when you run it the second time though I get an error on checking if it's a primary seat because SeatIDs is Nothing. When I set a breakpoint on curCustomer = Nothing it triggered the first time around, I changed the customers name to "test", put a breakpoint on the next line and it did set curCustomer to nothing like it should have. Next when I ran the code again it threw the error on checking the primary seat, and curCustomer still existed with the "test" name I gave it before setting it to Nothing. Why is this happening?

È stato utile?

Soluzione

The curCustomer = Nothing isn't actually doing anything (in the example) as curCustomer is re-initalized in the next for-each sequence and curCustomer function only as a reference (you are nulling the reference to the item in the array, not the array item itself).

You will need to use an index to set it to nothing in the array _customersEdit.

You could do:

    Dim i As Integer
    For i = 0 To _customersEdit.Count - 1
        ' your check code
        If (curCustomer.SeatIDs.Count = 0) Then
            _customerEdit(i) = Nothing
        End If

    Next

Or probably better:

    Dim i As Integer
    For i =  _customersEdit.Count - 1 To 0 Step -1
        ' your check code
        If (curCustomer.SeatIDs.Count = 0) Then
            _customerEdit.RemoveAt(i)
        End If
    Next

Altri suggerimenti

_customersEdit(i) = Nothing will set the reference in the array to Nothing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top