Collection Question

  • Thread starter Thread starter Brett Wesoloski
  • Start date Start date
B

Brett Wesoloski

I have a collection that I add my objects to. When I go to remove an object
from the collect I get an error {"Cannot remove the specified item because
it was not found in the specified Collection."}
Now I do understand what it is saying but I know the object is in the
collection.

Here is some code.


? CurrentEPCInfo

{KCC.MPAM.RFID.EPCCollection.EPCInfo}

_EPC: "30540232801ADB0000000006"

_ID: "Kleenex"

_Pic: "C:\\Visual Studio
Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg"

EPC: "30540232801ADB0000000006"

ID: "Kleenex"

PIC: "C:\\Visual Studio
Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg"



? CartEPCs[0]

{KCC.MPAM.RFID.EPCCollection.EPCInfo}

_EPC: "30540232801ADB0000000006"

_ID: "Kleenex"

_Pic: "C:\\Visual Studio
Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg"

EPC: "30540232801ADB0000000006"

ID: "Kleenex"

PIC: "C:\\Visual Studio
Projects\\RFID\\KCC.MPAM.RFID.RocketCart\\KCC.MPAM.RFID.RocketCart\\Pics\\KleenexTissue.jpg"



Now they are the same.


Here is the code I am using.


CurrentEPCInfo.ID = "Kleenex";

CurrentEPCInfo.EPC = "30540232801ADB0000000006";

CurrentEPCInfo.PIC = @"C:\Visual Studio
Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RFID.RocketCart\Pics\KleenexTissue.jpg";


CartEPCs.Remove(CurrentEPCInfo);

Now what really confuses me is if I do this

LoadedEPCs.Add(CurrentEPCInfo);

LoadedEPCs.Remove(CurrentEPCInfo);


Add it and then remove it right away it works.



Any idea's?



TIA,
Brett
 
When using reference types (and probably when using complex value types),
the Remove() method only removes the specific instance of an object. It doesn't
remove an instance that just happens to contain the same member values as
one already in the collection. To remove the specific item, you would first
need to get a reference to the actual instance in the collection, and then
call Remove(), passing it that reference. For instance, the following code
would scan through the list and remove the item you seek.
For Each scanItem As EPCInfoClass In LoadedEPCs
If (scanItem.EPC = "30540232801ADB0000000006") Then
LoadedEPCs.Remove(scanItem)
Exit For
End If
Next scanItem

There are other ways to accomplish this. You could override the Equals method
for the class and use it to locate matching items.
Public Overrides Function Equals(ByVal obj As Object) As Boolean
' ----- Allow IndexOf() and Contains() searches.
If (TypeOf obj Is String) Then
Return CBool(CStr(obj) = Me.EPC)
Else
Return MyBase.Equals(obj)
End If
End Function

Then you could use a statement like this.
 
Ah, thanks! That makes sense.


Tim Patrick said:
When using reference types (and probably when using complex value types),
the Remove() method only removes the specific instance of an object. It
doesn't remove an instance that just happens to contain the same member
values as one already in the collection. To remove the specific item, you
would first need to get a reference to the actual instance in the
collection, and then call Remove(), passing it that reference. For
instance, the following code would scan through the list and remove the
item you seek.


There are other ways to accomplish this. You could override the Equals
method for the class and use it to locate matching items.


Then you could use a statement like this.
 
Back
Top