Problem trying to "replace" an Object in a List(Of Object)

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
B

BobRoyAce

I have a class, MerchantList, which has a property, Merchants which is
a List(Of Merchant). I am trying to figure out how to "replace" one of
the Merchant objects in the Merchants List with another new one. I
created a simple test, coded as follows:

Dim oList As New MerchantList
Dim oMerchant As Merchant

' Add five Merchants to the listBoxControl1
For i As Integer = 1 To 5
oMerchant = New Merchant
oMerchant.pkMerchantID = i
oMerchant.MerchantLegalBusinessName = "Merchant #" & i.ToString
oList.Merchants.Add(oMerchant)
Next

' Report of BEFORE results...
Dim sTemp As String = "BEFORE: " & vbCrLf & "------" & vbCrLf
For Each oMerchant In oList.Merchants
sTemp += oMerchant.pkMerchantID.ToString & ": " &
oMerchant.MerchantLegalBusinessName & vbCrLf
Next

' Create a NEW Merchant
Dim oNewMerchant As New Merchant
oNewMerchant.pkMerchantID = 3
oNewMerchant.MerchantLegalBusinessName = "Merchant Three (NEW)"

' Report NEW Merchant...
sTemp += vbCrLf & "NEW: " & oNewMerchant.pkMerchantID.ToString &
": " & oNewMerchant.MerchantLegalBusinessName & vbCrLf

' Find NEW Merchant in list and replace one with matching
pkMerchantID
For Each oMerchant In oList.Merchants
If (oNewMerchant.pkMerchantID = oMerchant.pkMerchantID) Then
oMerchant = oNewMerchant
Exit For
End If
Next

'Report AFTER results...
sTemp += vbCrLf & "AFTER:" & vbCrLf & "-----" & vbCrLf
For Each oMerchant In oList.Merchants
sTemp += oMerchant.pkMerchantID.ToString & ": " &
oMerchant.MerchantLegalBusinessName & vbCrLf
Next

This simple test did not work as can be seen by the results reported
below:

BEFORE:
------
1: Merchant #1
2: Merchant #2
3: Merchant #3
4: Merchant #4
5: Merchant #5

NEW: 3: Merchant Three (NEW)

AFTER:
-----
1: Merchant #1
2: Merchant #2
3: Merchant #3
4: Merchant #4
5: Merchant #5

What am I doing wrong?
 
Back
Top