473,386 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

collection question

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.RF ID.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
Oct 24 '06 #1
6 1796
Brett Wesoloski wrote:
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
<snip>

That doesn't look like code. Could you post a short but complete
program that demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for more information.

Jon

Oct 24 '06 #2
n!
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.

Have you tried placing a breakpoint on all the places you remove an item
from the collection to make sure you haven't tried to remove it more than
once?

What kind of collection are you using?

n!
Oct 24 '06 #3
I'm guessing that this is due to the [equality] comparer; the vanilla object
comparer (object.Equals(object)) tests for referential integrity... i.e.
things are equal if the *reference* is equal, i.e. they are exactly the same
instance.

I suspect you have two separate instances with the same data. For this to
work, you should really provide your own equality implementation (override
Equals), and (always hand-in-hand) hash-code.

From your example, I'm guessing that EPC is the key value here, so you could
just (in the comparar) compare the EPC values of the two items; for
GetHashCode(), return EPC.GetHashCode().

In 2.0, you can also implement IEquatable<T>, which prives the type-safe
Equals(T), however this should be in addition to overriding Equals. You
might also want to look at providing suitable == and != operators.

But (very, very important); a conformant Equals implementation *must* go
hand in hand with GetHashCode(); any two instances that "equate" *must*
return the same hash code. The reverse is not necessary. Ideally, the
hash-code would be immutable as well... i.e. if you use "EPC" for both
equality and hashing, then this would be a readonly field.

Marc
Oct 24 '06 #4
Sorry

Here is my code to remove the object, the collection is in VB.NET
Public Sub Remove(ByVal value As EPCInfo)

List.Remove(value)

End Sub

Code to add the object

Public Function Add(ByVal value As EPCInfo) As Integer

Return List.Add(value)

End Function

Then in my C# code I add the items to the collection.

private void button1_Click(object sender, EventArgs e)

{

EPCInfo EPC1 = new EPCInfo();

EPC1.ID = "Kleenex";

EPC1.EPC = "30540232801ADB0000000006";

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

CartEPCs.Add(EPC1);

EPCInfo EPC2 = new EPCInfo();

EPC2.ID = "Huggies";

EPC2.EPC = "305402328074620000000001";

EPC2.PIC = @"C:\Visual Studio Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RF ID.RocketCart\Pics\HuggiesDiapers.jpg";

CartEPCs.Add(EPC2);

EPCInfo EPC3 = new EPCInfo();

EPC3.ID = "Cottonelle";

EPC3.EPC = "3054023280A0F20000000002";

EPC3.PIC = @"C:\Visual Studio Projects\RFID\KCC.MPAM.RFID.RocketCart\KCC.MPAM.RF ID.RocketCart\Pics\CottonelleRipples.jpg";

CartEPCs.Add(EPC3);

}

Lastly I try to remove an item from the collection. This is where I get the error.



private void button1_Click(object sender, EventArgs e)

{

EPCInfo CurrentEPCInfo = new EPCInfo();

CurrentEPCInfo.ID = "Huggies";

CurrentEPCInfo.EPC = "305402328074620000000001";

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

CartEPCs.Remove(CurrentEPCInfo);

}

But when I look through the collection of CartEPCs I see the info.

Also if I do an add to the CartEPCs and then a remove it works fine.

Brett

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message news:11**********************@i3g2000cwc.googlegro ups.com...
Brett Wesoloski wrote:
>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
<snip>

That doesn't look like code. Could you post a short but complete
program that demonstrates the problem? See
http://www.pobox.com/~skeet/csharp/complete.html for more information.

Jon
Oct 24 '06 #5
March,

Thanks this helps alot and make a lot of sense.

Brett
"Marc Gravell" <ma**********@gmail.comwrote in message
news:Oy*************@TK2MSFTNGP03.phx.gbl...
I'm guessing that this is due to the [equality] comparer; the vanilla
object comparer (object.Equals(object)) tests for referential integrity...
i.e. things are equal if the *reference* is equal, i.e. they are exactly
the same instance.

I suspect you have two separate instances with the same data. For this to
work, you should really provide your own equality implementation (override
Equals), and (always hand-in-hand) hash-code.

From your example, I'm guessing that EPC is the key value here, so you
could just (in the comparar) compare the EPC values of the two items; for
GetHashCode(), return EPC.GetHashCode().

In 2.0, you can also implement IEquatable<T>, which prives the type-safe
Equals(T), however this should be in addition to overriding Equals. You
might also want to look at providing suitable == and != operators.

But (very, very important); a conformant Equals implementation *must* go
hand in hand with GetHashCode(); any two instances that "equate" *must*
return the same hash code. The reverse is not necessary. Ideally, the
hash-code would be immutable as well... i.e. if you use "EPC" for both
equality and hashing, then this would be a readonly field.

Marc

Oct 24 '06 #6
Brett Wesoloski <br*************@kcc.omwrote:
Here is my code to remove the object, the collection is in VB.NET
See http://www.pobox.com/~skeet/csharp/incomplete.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 24 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from the container or whether it also gets deleted in...
5
by: Kurt Bauer | last post by:
I have an ASP group calendar application which pulls calendar data from Exchange via webdav into an XML string. I then loop the XML nodes to populate a collection of appointments. Finally I use...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
7
by: Pete Davis | last post by:
A different question this time. I have a DataGrid bound to a collection. Is there any way for me to allow sorting? The DataGrid.AllowSorting=true doesn't work, but that's probably because it can't...
4
by: bkazlak | last post by:
Hello, I have a quick question might help me understand garbage collection. let's say I'm having a static collection of objects in one class, so this collection should be cached and present...
3
by: JJ | last post by:
Hi, I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering...
16
by: Ben Hannon | last post by:
Hi, I'm writting a COM Class in VB.NET to be used in a VB6 project (Tired of the VB6 hassles with cloning and serializing an object). All my classes I need cloneable/serializable are now in a...
34
by: Craig Buchanan | last post by:
Which vb.net object is the best match for the vb6 collection class? Specifically, I would like to be able to access the Item property with an index or a key string. I wrote my own class that...
54
by: MLH | last post by:
I use A97 and do not always insert line numbers while writing procedures. I find it necessary to go back and add them later to aid in debugging. Nearly 3 years ago, something was mentioned in...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.