Storing objects by ref

J

Joe

This my seems stupid because it should be very obvious but I'm going to ask
anyway...

I have a class MyCollection which inherits from CollectionBase and another
class MyObject which is stored in MyCollection and yet another class
MyOtherObject which references MyObject from MyCollection like this:

MyCollection.Add(new MyObject("somename") );

MyOtherObject.myObject = MyCollection["somename"];

The problem I get is the instance that MyOtherObject.myObject seems to be a
copy of the original object from the collection and not a reference to it. I
noticed this because I changed a member of the object from the collection
but the MyOtherObject.myObject doesn't reflect the changes.

I thought that .NET typically passes objects by reference.

Maybe I'm working too much and just drawing blanks...:)

Thanks,
Joe
 
N

Nicholas Paldino [.NET/C# MVP]

Joe,

Assuming that you aren't doing anything special when you are adding the
item (which you are, since you are obviously keying the collection based on
a property of MyObject), and that MyObject is a class, and not a structure,
you are getting the same object (again, assuming that your indexer doesn't
do anything strange).

Without seeing your collection object, it is hard to tell.

But what makes you think that you are not getting the same object?
 
J

Joe

Hi Nicholas,

The problem is not with the collection but with the object that stores a
reference to the item in the collection.

I think I figured out where the problem is happening but not how to fix it.

My classes are being serialized and written to a file. When I deserialize
them, change an object in the collection and than check the reference to
that object in MyOtherObject.myOjbect I see that this is different. For one
some of the fields should be null and they are not. All other members still
have their old values.

So I guess the question than becomes, How can I update an object once its
been deserialized and have the originally referenced locations see that
change?

-Joe

Nicholas Paldino said:
Joe,

Assuming that you aren't doing anything special when you are adding the
item (which you are, since you are obviously keying the collection based
on a property of MyObject), and that MyObject is a class, and not a
structure, you are getting the same object (again, assuming that your
indexer doesn't do anything strange).

Without seeing your collection object, it is hard to tell.

But what makes you think that you are not getting the same object?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
This my seems stupid because it should be very obvious but I'm going to
ask anyway...

I have a class MyCollection which inherits from CollectionBase and
another class MyObject which is stored in MyCollection and yet another
class MyOtherObject which references MyObject from MyCollection like
this:

MyCollection.Add(new MyObject("somename") );

MyOtherObject.myObject = MyCollection["somename"];

The problem I get is the instance that MyOtherObject.myObject seems to be
a copy of the original object from the collection and not a reference to
it. I noticed this because I changed a member of the object from the
collection but the MyOtherObject.myObject doesn't reflect the changes.

I thought that .NET typically passes objects by reference.

Maybe I'm working too much and just drawing blanks...:)

Thanks,
Joe
 
N

Nicholas Paldino [.NET/C# MVP]

Oh, you can't do that. You have to know where these references are
being kept, and then patch it up yourself. Either that, or have some sort
of wrapper which you keep a referenece to, while the internal reference is
changed upon deserialization.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
Hi Nicholas,

The problem is not with the collection but with the object that stores a
reference to the item in the collection.

I think I figured out where the problem is happening but not how to fix
it.

My classes are being serialized and written to a file. When I deserialize
them, change an object in the collection and than check the reference to
that object in MyOtherObject.myOjbect I see that this is different. For
one some of the fields should be null and they are not. All other members
still have their old values.

So I guess the question than becomes, How can I update an object once its
been deserialized and have the originally referenced locations see that
change?

-Joe

Nicholas Paldino said:
Joe,

Assuming that you aren't doing anything special when you are adding
the item (which you are, since you are obviously keying the collection
based on a property of MyObject), and that MyObject is a class, and not a
structure, you are getting the same object (again, assuming that your
indexer doesn't do anything strange).

Without seeing your collection object, it is hard to tell.

But what makes you think that you are not getting the same object?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
This my seems stupid because it should be very obvious but I'm going to
ask anyway...

I have a class MyCollection which inherits from CollectionBase and
another class MyObject which is stored in MyCollection and yet another
class MyOtherObject which references MyObject from MyCollection like
this:

MyCollection.Add(new MyObject("somename") );

MyOtherObject.myObject = MyCollection["somename"];

The problem I get is the instance that MyOtherObject.myObject seems to
be a copy of the original object from the collection and not a reference
to it. I noticed this because I changed a member of the object from the
collection but the MyOtherObject.myObject doesn't reflect the changes.

I thought that .NET typically passes objects by reference.

Maybe I'm working too much and just drawing blanks...:)

Thanks,
Joe
 
J

Joe

I figured that...

If I understand this correctly than, deserialization actually creates a new
object from the original serialized object. This in-turn cause a higher
memory usage.

I'm going to need to redesign some of my code now to take into account this
issue.

Thanks for your time.

Joe

Nicholas Paldino said:
Oh, you can't do that. You have to know where these references are
being kept, and then patch it up yourself. Either that, or have some sort
of wrapper which you keep a referenece to, while the internal reference is
changed upon deserialization.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Joe said:
Hi Nicholas,

The problem is not with the collection but with the object that stores a
reference to the item in the collection.

I think I figured out where the problem is happening but not how to fix
it.

My classes are being serialized and written to a file. When I deserialize
them, change an object in the collection and than check the reference to
that object in MyOtherObject.myOjbect I see that this is different. For
one some of the fields should be null and they are not. All other members
still have their old values.

So I guess the question than becomes, How can I update an object once its
been deserialized and have the originally referenced locations see that
change?

-Joe

Nicholas Paldino said:
Joe,

Assuming that you aren't doing anything special when you are adding
the item (which you are, since you are obviously keying the collection
based on a property of MyObject), and that MyObject is a class, and not
a structure, you are getting the same object (again, assuming that your
indexer doesn't do anything strange).

Without seeing your collection object, it is hard to tell.

But what makes you think that you are not getting the same object?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

This my seems stupid because it should be very obvious but I'm going to
ask anyway...

I have a class MyCollection which inherits from CollectionBase and
another class MyObject which is stored in MyCollection and yet another
class MyOtherObject which references MyObject from MyCollection like
this:

MyCollection.Add(new MyObject("somename") );

MyOtherObject.myObject = MyCollection["somename"];

The problem I get is the instance that MyOtherObject.myObject seems to
be a copy of the original object from the collection and not a
reference to it. I noticed this because I changed a member of the
object from the collection but the MyOtherObject.myObject doesn't
reflect the changes.

I thought that .NET typically passes objects by reference.

Maybe I'm working too much and just drawing blanks...:)

Thanks,
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top