J
Jeff Dege
I'm trying to do something fairly simple, and I'm generating nothing but
confusion.
Does Dictionary.ContainsKey() compare pointers, or values? I have
defined two classes, FooId, and FooData. With these, I've declared a
fooDictionary Dictionary collection. I then run a database query, and
loop on a SqlDataReader object.
In each loop, I construct a new FooId object and a new FooData object.
I then check fooDictionary.ContainsKey(fooId). This returns true, when
there is already a FooId object with the same values as the new FooId
object I've just created. If so, I create a new FooData object, and add
it into the collection.
My initial problem was that I wanted to walk down the collection, calling
a method on every FooData object, when that method would modify the member
fields of the FooData object in the collection. And I simply couldn't
find a way for that to work.
If I called the method on the object in place, it would seem to work, but
the object in the collection would not actually be modified. It's like if
I did a foreach(KeyValuePair<> kvp in ), kvp.Value was giving me a copy of
the object in the collection, rather than a reference to the object. And
if I tried to replace the object, I'd get a "collection modified"
exception thrown.
So I started to put together a simple example, showing what was going on.
But I couldn't do it. First, with my simple example, the
foreach(KeyValuePair) worked - and did modify the object in the
collection. But calling Dictionary.ContainsKey() on a newly constructed
object did not - FooId objects with identical values would never match,
and I'd end up with a Dictionary containing multiple keys with identical
member fields.
I don't understand it. Why would a Dictionary.ContainsKey() seem to be
doing value comparisons with the classes I was building from the
SqlDataReader, but be doing pointer comparisons with the simple classes I
was building for my demo?
And why would KeyValuePair.Value return a copy of the object in the
collection, when that object was constructed from the SqlDataReader, but
return a reference to the object in the collection, when it was
constructed from my simple demo data?
It's purely a befuddlement.
--
The police are the public and the public are the police; the police being
only members of the public who are paid to give full time attention to
duties which are incumbent on every citizen in the interests of community
welfare and existence.
-Sir Robert Peel.
confusion.
Does Dictionary.ContainsKey() compare pointers, or values? I have
defined two classes, FooId, and FooData. With these, I've declared a
fooDictionary Dictionary collection. I then run a database query, and
loop on a SqlDataReader object.
In each loop, I construct a new FooId object and a new FooData object.
I then check fooDictionary.ContainsKey(fooId). This returns true, when
there is already a FooId object with the same values as the new FooId
object I've just created. If so, I create a new FooData object, and add
it into the collection.
My initial problem was that I wanted to walk down the collection, calling
a method on every FooData object, when that method would modify the member
fields of the FooData object in the collection. And I simply couldn't
find a way for that to work.
If I called the method on the object in place, it would seem to work, but
the object in the collection would not actually be modified. It's like if
I did a foreach(KeyValuePair<> kvp in ), kvp.Value was giving me a copy of
the object in the collection, rather than a reference to the object. And
if I tried to replace the object, I'd get a "collection modified"
exception thrown.
So I started to put together a simple example, showing what was going on.
But I couldn't do it. First, with my simple example, the
foreach(KeyValuePair) worked - and did modify the object in the
collection. But calling Dictionary.ContainsKey() on a newly constructed
object did not - FooId objects with identical values would never match,
and I'd end up with a Dictionary containing multiple keys with identical
member fields.
I don't understand it. Why would a Dictionary.ContainsKey() seem to be
doing value comparisons with the classes I was building from the
SqlDataReader, but be doing pointer comparisons with the simple classes I
was building for my demo?
And why would KeyValuePair.Value return a copy of the object in the
collection, when that object was constructed from the SqlDataReader, but
return a reference to the object in the collection, when it was
constructed from my simple demo data?
It's purely a befuddlement.
--
The police are the public and the public are the police; the police being
only members of the public who are paid to give full time attention to
duties which are incumbent on every citizen in the interests of community
welfare and existence.
-Sir Robert Peel.