PC Review


Reply
Thread Tools Rate Thread

Conceptual question--How do collections store objects in .NET ?

 
 
Kevin
Guest
Posts: n/a
 
      13th Feb 2007
I'm missing a basic understanding of how .net stores objects in a
collection. For example, I have an object called "dog", and I would
like to create and populate a List<> of these.

For example:.
dog poodle =new dog("small", "white", 1);
dog shepherd =new dog("big", "brown", 2);
dog lab =new dog("medium", "black",3);

List<dog> allDogs=new List<dog>();

allDogs.Add(poodle);
allDogs.Add(shepherd);
allDogs.Add(lab);

Now, I would like a second list that overlaps with part of the first
list:

List<dog> bigDogs=new List<dog>();
bigDogs.Add(shepherd);

What exactly is hapening here? Is 'bigDogs' merely getting a
reference to 'shepherd', or is an entirely new copy of 'shepherd'
created for inclusion in the 'bigDogs' list?

If I change one item in the allDogs list (say, allDogs[1].color =
"Black") this change is not reflected in the bigDogs list. I must
assume that there are now multiple copies of shepherd...

This is unfortunate. Is there a way to create a collection that
simply refers to objects by reference? Or, put another way, is there
any way to have two overlapping lists (or some other collection) so
that a change to an item in one list is reflected in the other
list?

Thanks in advance.
-Kevin

 
Reply With Quote
 
 
 
 
John B
Guest
Posts: n/a
 
      13th Feb 2007
Kevin wrote:
> I'm missing a basic understanding of how .net stores objects in a
> collection. For example, I have an object called "dog", and I would
> like to create and populate a List<> of these.
>
> For example:.
> dog poodle =new dog("small", "white", 1);
> dog shepherd =new dog("big", "brown", 2);
> dog lab =new dog("medium", "black",3);
>
> List<dog> allDogs=new List<dog>();
>
> allDogs.Add(poodle);
> allDogs.Add(shepherd);
> allDogs.Add(lab);
>
> Now, I would like a second list that overlaps with part of the first
> list:
>
> List<dog> bigDogs=new List<dog>();
> bigDogs.Add(shepherd);
>
> What exactly is hapening here? Is 'bigDogs' merely getting a
> reference to 'shepherd', or is an entirely new copy of 'shepherd'
> created for inclusion in the 'bigDogs' list?
>

The first, it adds a reference.
Unless its a value type (or immutable ref type) then it will add a copy.
Is dog a struct?

> If I change one item in the allDogs list (say, allDogs[1].color =
> "Black") this change is not reflected in the bigDogs list. I must
> assume that there are now multiple copies of shepherd...
>

There must be something strange going on there.
Set shepherd.color="Black" and check all three references to see if
they've changed, they will have unless something else is going on.

<...>

JB
 
Reply With Quote
 
cristalink
Guest
Posts: n/a
 
      13th Feb 2007
> What exactly is hapening here? Is 'bigDogs' merely getting a
> reference to 'shepherd', or is an entirely new copy of 'shepherd'
> created for inclusion in the 'bigDogs' list?


If "dog" is a class, then List stores the references. If dog is a struct
(which is apprarently your case), then the lists will contain independent
copies of the objects.

--

"Kevin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I'm missing a basic understanding of how .net stores objects in a
> collection. For example, I have an object called "dog", and I would
> like to create and populate a List<> of these.
>
> For example:.
> dog poodle =new dog("small", "white", 1);
> dog shepherd =new dog("big", "brown", 2);
> dog lab =new dog("medium", "black",3);
>
> List<dog> allDogs=new List<dog>();
>
> allDogs.Add(poodle);
> allDogs.Add(shepherd);
> allDogs.Add(lab);
>
> Now, I would like a second list that overlaps with part of the first
> list:
>
> List<dog> bigDogs=new List<dog>();
> bigDogs.Add(shepherd);
>
> What exactly is hapening here? Is 'bigDogs' merely getting a
> reference to 'shepherd', or is an entirely new copy of 'shepherd'
> created for inclusion in the 'bigDogs' list?
>
> If I change one item in the allDogs list (say, allDogs[1].color =
> "Black") this change is not reflected in the bigDogs list. I must
> assume that there are now multiple copies of shepherd...
>
> This is unfortunate. Is there a way to create a collection that
> simply refers to objects by reference? Or, put another way, is there
> any way to have two overlapping lists (or some other collection) so
> that a change to an item in one list is reflected in the other
> list?
>
> Thanks in advance.
> -Kevin
>



 
Reply With Quote
 
Kevin
Guest
Posts: n/a
 
      13th Feb 2007
On Feb 12, 6:22 pm, John B <jbngs...@yahoo.com> wrote:
> Kevin wrote:
> > I'm missing a basic understanding of how .net stores objects in a
> > collection. For example, I have an object called "dog", and I would
> > like to create and populate a List<> of these.

>
> > For example:.
> > dog poodle =new dog("small", "white", 1);
> > dog shepherd =new dog("big", "brown", 2);
> > dog lab =new dog("medium", "black",3);

>
> > List<dog> allDogs=new List<dog>();

>
> > allDogs.Add(poodle);
> > allDogs.Add(shepherd);
> > allDogs.Add(lab);

>
> > Now, I would like a second list that overlaps with part of the first
> > list:

>
> > List<dog> bigDogs=new List<dog>();
> > bigDogs.Add(shepherd);

>
> > What exactly is hapening here? Is 'bigDogs' merely getting a
> > reference to 'shepherd', or is an entirely new copy of 'shepherd'
> > created for inclusion in the 'bigDogs' list?

>
> The first, it adds a reference.
> Unless its a value type (or immutable ref type) then it will add a copy.
> Is dog a struct?
>
> > If I change one item in the allDogs list (say, allDogs[1].color =
> > "Black") this change is not reflected in the bigDogs list. I must
> > assume that there are now multiple copies of shepherd...

>
> There must be something strange going on there.
> Set shepherd.color="Black" and check all three references to see if
> they've changed, they will have unless something else is going on.
>
> <...>
>
> JB


Hi JB

Thank you for the clarification.

You are in fact correct--there was something strange going on, and it
was human error on my part. After walking away and coming back, I
found my error and discovered that yes, as you stated, the change was
being reflected properly. Some days trying harder just makes things
worse.

Thanks for your help and explanation.
-Kevin

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      13th Feb 2007
John B <(E-Mail Removed)> wrote:

<snip>

> The first, it adds a reference.
> Unless its a value type (or immutable ref type) then it will add a copy.
> Is dog a struct?


Immutable reference types are just like mutable reference types - no
copy is taken. It just so happens that you can't change the data within
the object. Indeed, it would be supremely pointless to take a copy of
the object given that its contents can't be changed anyway!

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
John B
Guest
Posts: n/a
 
      13th Feb 2007
Jon Skeet [C# MVP] wrote:
> John B <(E-Mail Removed)> wrote:
>
> <snip>
>
>> The first, it adds a reference.
>> Unless its a value type (or immutable ref type) then it will add a copy.
>> Is dog a struct?

>
> Immutable reference types are just like mutable reference types - no
> copy is taken. It just so happens that you can't change the data within
> the object. Indeed, it would be supremely pointless to take a copy of
> the object given that its contents can't be changed anyway!
>

Doh!
Right as always

Thanks

JB
 
Reply With Quote
 
susiedba@hotmail.com
Guest
Posts: n/a
 
      14th Feb 2007
use a database you ****ing idiot


I mean seriously here; arrays and all this other crap are a load of BS


On Feb 12, 5:35 pm, "Kevin" <kdried...@gmail.com> wrote:
> I'm missing a basic understanding of how .net stores objects in a
> collection. For example, I have an object called "dog", and I would
> like to create and populate a List<> of these.
>
> For example:.
> dog poodle =new dog("small", "white", 1);
> dog shepherd =new dog("big", "brown", 2);
> dog lab =new dog("medium", "black",3);
>
> List<dog> allDogs=new List<dog>();
>
> allDogs.Add(poodle);
> allDogs.Add(shepherd);
> allDogs.Add(lab);
>
> Now, I would like a second list that overlaps with part of the first
> list:
>
> List<dog> bigDogs=new List<dog>();
> bigDogs.Add(shepherd);
>
> What exactly is hapening here? Is 'bigDogs' merely getting a
> reference to 'shepherd', or is an entirely new copy of 'shepherd'
> created for inclusion in the 'bigDogs' list?
>
> If I change one item in the allDogs list (say, allDogs[1].color =
> "Black") this change is not reflected in the bigDogs list. I must
> assume that there are now multiple copies of shepherd...
>
> This is unfortunate. Is there a way to create a collection that
> simply refers to objects by reference? Or, put another way, is there
> any way to have two overlapping lists (or some other collection) so
> that a change to an item in one list is reflected in the other
> list?
>
> Thanks in advance.
> -Kevin



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conceptual - Communication between Objects RSH Microsoft C# .NET 7 30th May 2007 03:46 PM
Learning OOP conceptual question RSH Microsoft VB .NET 12 12th Nov 2006 05:54 PM
Learning OOP conceptual question(s) RSH Microsoft C# .NET 6 9th Nov 2006 02:23 PM
Re: Conceptual Question about ReDim Chip Pearson Microsoft Excel Programming 0 14th Apr 2005 03:50 PM
Re: new guy with a conceptual question Nigel Microsoft Excel Programming 0 5th Mar 2004 06:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:24 AM.