Home All Groups Group Topic Archive Search About
Author
24 Oct 2006 3:54 PM
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

Author
24 Oct 2006 4:16 PM
Tim Patrick
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.

> LoadedEPCs.RemoveAt(LoadedEPCs.IndexOf("30540232801ADB0000000006"))

-----
Tim Patrick
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> 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 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\K
> leenexTissue.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
Author
24 Oct 2006 4:35 PM
Brett Wesoloski
Ah, thanks!  That makes sense.


Show quoteHide quote
"Tim Patrick" <inva***@invalid.com.invalid> wrote in message
news:e3b469760cd98c8c564446a6bc2@newsgroups.comcast.net...
> 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.
>
>> LoadedEPCs.RemoveAt(LoadedEPCs.IndexOf("30540232801ADB0000000006"))
>
> -----
> Tim Patrick
> Start-to-Finish Visual Basic 2005
>
>> 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 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\K
>> leenexTissue.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
>
>