Collection woes

  • Thread starter Thread starter jdn
  • Start date Start date
J

jdn

I have a class which inherits from System.Collections.CollectionBase and
am having one heck of a time figuring the following out.

The point of the class is to hold objects of another class.

Specifically, I have an Identity class which holds various types of
information, and the other class is the Identities class. It's only job
is to hold a collection of Identity objects.

Since it inherits from CollectionBase, I know I shouldn't need to do
this, but I added a private member ArrayList called m_IdentityArrayList.

I have the following function to add an identity object:

public Identity Add(Identity objAddIdentity){
m_IdentityArrayList.Add(objAddIdentity);
return objAddIdentity;
}

I return the object in case I need to do something to it after adding
it, though I don't at the moment.

Here's the issue:

I add an Identity object. While in debug mode I can view its properties
just fine and everything looks okay. Then I add a second object. When
I look at the properties for the second Identity object after adding it,
it looks fine. But then I look at the first Identity object, and it now
has the properties of the second object, not its original properties.
If I add a third Identity object, then the properties of each Identity
object are only of the third object.

So, the collection seems to overwrite the properties of each already
existing object with the properties of the last one added.

What could I possibly be doing wrong?

jdn
 
jdn said:
I have a class which inherits from System.Collections.CollectionBase and
am having one heck of a time figuring the following out.

The point of the class is to hold objects of another class.

Specifically, I have an Identity class which holds various types of
information, and the other class is the Identities class. It's only job
is to hold a collection of Identity objects.

Since it inherits from CollectionBase, I know I shouldn't need to do
this, but I added a private member ArrayList called m_IdentityArrayList.

I have the following function to add an identity object:

public Identity Add(Identity objAddIdentity){
m_IdentityArrayList.Add(objAddIdentity);
return objAddIdentity;
}

I return the object in case I need to do something to it after adding
it, though I don't at the moment.

Here's the issue:

I add an Identity object. While in debug mode I can view its properties
just fine and everything looks okay. Then I add a second object. When
I look at the properties for the second Identity object after adding it,
it looks fine. But then I look at the first Identity object, and it now
has the properties of the second object, not its original properties. If
I add a third Identity object, then the properties of each Identity
object are only of the third object.

So, the collection seems to overwrite the properties of each already
existing object with the properties of the last one added.

What could I possibly be doing wrong?

jdn


Okay, type it and then the solution occurs to you.

Each Identity object has sub-objects in them, and somehow, even though
it looked in debug mode like the sub-objects had the right 'new' values
(I have methods that should update the sub-objects' values), when
adding, it was retaining the old ones. But explicitly doing a new
constructor and then getting the sub-object data, that worked.

So, there's another code issue, but this one is resolved.

jdn
 
jdn said:
Okay, type it and then the solution occurs to you.

Each Identity object has sub-objects in them, and somehow, even though
it looked in debug mode like the sub-objects had the right 'new' values
(I have methods that should update the sub-objects' values), when
adding, it was retaining the old ones. But explicitly doing a new
constructor and then getting the sub-object data, that worked.

So, there's another code issue, but this one is resolved.

jdn

Sorry, I wrote that wrong.

It appears that when I was doing the update of the data of the
sub-objects, even though it appeared I was only updating the newest
Identity object, it was also updating all existing ones.

So, the updating code was working well...all too well.....

Reminder to self...coding after midnight...bad.

jdn
 
Let me add something.

First, you should not have to create your own ArrayList to hold your values.
If you look at the CollectionBase, you will see that there are protected
members used for storing the information. I think this is necessary because
if you cast your collection to a CollectionBase object, since your Add
method is not virtual, you will not be able to see any of your objects. In
fact, a better thing to do might be to delegate all commands to the base
(CollectionBase) class. So if someone calls your Add method, you could
simply call base.Add(...), same with remove and all the other methods.

Second, the Add method does not need to return the object that it added. If
you look at the signature for the CollectionBase Add method, it returns an
integer indicating the ordinal position within the list.
 
Back
Top