How to "replace" an Object in a List(Of Object)

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?
 
F

Family Tree Mike

I believe what your code is doing is setting the reference of the original
Merchant object to point to the new Merchant object. Unfortunately this does
not change the reference of the list entry.

I think the code is more logical to delete the old Merchant from the list,
and add the new Merchant to the list.
 
B

Bill McCarthy

Hi Bob,

You need to locate the item in the list and replace it. Note: do NOT do this
during an iteration of the list as that can cause exceptions as you are
changing the list you are iterating. Instead, find the location, then
replace that item. eg:

Dim pos As Int32 = -1
For i as Int32 = 0 to oList.Count - 1
If oList(i).pkMerchantID = oMerchant.pkMerchantID) Then
pos = i
Exit For
End If
Next

If pos > -1 Then
' existing record found
oList(i) = oMerchant
Else
' add new record
oList.Add(oMerchant)
End If


As you appear to have a cusotm list class, MerchantList, I would suggest you
wrap this code into a method such as Replace or and ReplaceOrAdd or smehtign
like that.
 
M

Mr. Arnold

I think you're going to have to do it with a For Loop using an IndexCnt.

Maybe it would be something like this.


For (i = 0 to oList.Merchants.Count)
If ( oMerchant.items(i).pkMerchantID = oNewMerchant.pkMerchantID) Then
oMerchant.items(i) = oNewMerchant
exitfor
endif

Next


I know in C#.net that it would flag it as an compile error when you try to
replace an item using the For Each loop with a message saying you cannot
replace an object that's being referenced/used in the For Each loop.

For Each oMerchant In oList.Merchants

And it's apparent the VB is not letting you get away with it, but it's not
stopping you with a compile error.
 
B

BobRoyAce

Thanks for your help. Now, I understand. If I create a function to do
it, which I'd like to do, should I pass the Merchant object I want to
replace or add ByRef or ByVal? I am thinking ByVal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top