PC Review


Reply
Thread Tools Rate Thread

Change content of a List<T>?

 
 
=?Utf-8?B?R2FyeQ==?=
Guest
Posts: n/a
 
      31st Aug 2006
I am using List<MyClass> to store a bunch of MyClasses. And in my program, I
want to change the content of one class in this list. However, it seems to me
that this list keeps an original copy and is only "readable".

Following is my code:

int index = GetMyClassIndFromName(string);

if (index != -1)
{
MyClass mf = m_allMyClass[index];
mf.Available = true;
}

I think the mf is just a temporary copy of that MyClass. How may I change
the original content?

Thanks,
 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      31st Aug 2006
Gary,
Let's clear this up with a handy-dandy little console app you can run:

using System;
using System.Collections.Generic;
using System.Text;
namespace ListMyClass
{
public class MyClass
{
public int Id;
public string Name;
public MyClass(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
class Program
{
static void Main(string[] args)
{
List<MyClass> myClasses= new List<MyClass>();
myClasses.Add(new MyClass(1, "Testing1"));
myClasses.Add(new MyClass(2, "Testing2"));
MyClass test = myClasses[0];
test.Name = "BORK!";
test.Id = 1229;
MyClass test2 = myClasses[0];
Console.WriteLine(test2.Id.ToString() + ": " + test2.Name);
Console.ReadLine();
}
}
}

--Enjoy.
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Gary" wrote:

> I am using List<MyClass> to store a bunch of MyClasses. And in my program, I
> want to change the content of one class in this list. However, it seems to me
> that this list keeps an original copy and is only "readable".
>
> Following is my code:
>
> int index = GetMyClassIndFromName(string);
>
> if (index != -1)
> {
> MyClass mf = m_allMyClass[index];
> mf.Available = true;
> }
>
> I think the mf is just a temporary copy of that MyClass. How may I change
> the original content?
>
> Thanks,

 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      31st Aug 2006
Gary,

Is by any chance MyClass decalred as a structure?
Otherwise there is no reason to get a copy of the actual value.

Can you post a simple, compilable sample that demonstrates the problem?


--
Stoitcho Goutsev (100)



"Gary" <(E-Mail Removed)> wrote in message
news:BE8B04EA-1C2D-46F4-8D58-(E-Mail Removed)...
>I am using List<MyClass> to store a bunch of MyClasses. And in my program,
>I
> want to change the content of one class in this list. However, it seems to
> me
> that this list keeps an original copy and is only "readable".
>
> Following is my code:
>
> int index = GetMyClassIndFromName(string);
>
> if (index != -1)
> {
> MyClass mf = m_allMyClass[index];
> mf.Available = true;
> }
>
> I think the mf is just a temporary copy of that MyClass. How may I change
> the original content?
>
> Thanks,



 
Reply With Quote
 
=?Utf-8?B?R2FyeQ==?=
Guest
Posts: n/a
 
      1st Sep 2006
Yes. It is a structure actually. I believe that caused the problem.

Thanks,

"Stoitcho Goutsev (100)" wrote:

> Gary,
>
> Is by any chance MyClass decalred as a structure?
> Otherwise there is no reason to get a copy of the actual value.
>
> Can you post a simple, compilable sample that demonstrates the problem?
>
>
> --
> Stoitcho Goutsev (100)
>
>
>
> "Gary" <(E-Mail Removed)> wrote in message
> news:BE8B04EA-1C2D-46F4-8D58-(E-Mail Removed)...
> >I am using List<MyClass> to store a bunch of MyClasses. And in my program,
> >I
> > want to change the content of one class in this list. However, it seems to
> > me
> > that this list keeps an original copy and is only "readable".
> >
> > Following is my code:
> >
> > int index = GetMyClassIndFromName(string);
> >
> > if (index != -1)
> > {
> > MyClass mf = m_allMyClass[index];
> > mf.Available = true;
> > }
> >
> > I think the mf is just a temporary copy of that MyClass. How may I change
> > the original content?
> >
> > Thanks,

>
>
>

 
Reply With Quote
 
Stoitcho Goutsev \(100\)
Guest
Posts: n/a
 
      5th Sep 2006
Yes, value types are always copied.


--
HTH
Stoitcho Goutsev (100)

"Gary" <(E-Mail Removed)> wrote in message
news:CDE80A5B-ACDB-4CB3-B751-(E-Mail Removed)...
> Yes. It is a structure actually. I believe that caused the problem.
>
> Thanks,
>
> "Stoitcho Goutsev (100)" wrote:
>
>> Gary,
>>
>> Is by any chance MyClass decalred as a structure?
>> Otherwise there is no reason to get a copy of the actual value.
>>
>> Can you post a simple, compilable sample that demonstrates the problem?
>>
>>
>> --
>> Stoitcho Goutsev (100)
>>
>>
>>
>> "Gary" <(E-Mail Removed)> wrote in message
>> news:BE8B04EA-1C2D-46F4-8D58-(E-Mail Removed)...
>> >I am using List<MyClass> to store a bunch of MyClasses. And in my
>> >program,
>> >I
>> > want to change the content of one class in this list. However, it seems
>> > to
>> > me
>> > that this list keeps an original copy and is only "readable".
>> >
>> > Following is my code:
>> >
>> > int index = GetMyClassIndFromName(string);
>> >
>> > if (index != -1)
>> > {
>> > MyClass mf = m_allMyClass[index];
>> > mf.Available = true;
>> > }
>> >
>> > I think the mf is just a temporary copy of that MyClass. How may I
>> > change
>> > the original content?
>> >
>> > Thanks,

>>
>>
>>



 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      6th Sep 2006
"Gary" <(E-Mail Removed)> a écrit dans le message de news:
BE8B04EA-1C2D-46F4-8D58-(E-Mail Removed)...

|I am using List<MyClass> to store a bunch of MyClasses. And in my program,
I
| want to change the content of one class in this list. However, it seems to
me
| that this list keeps an original copy and is only "readable".
|
| Following is my code:
|
| int index = GetMyClassIndFromName(string);
|
| if (index != -1)
| {
| MyClass mf = m_allMyClass[index];
| mf.Available = true;
| }
|
| I think the mf is just a temporary copy of that MyClass. How may I change
| the original content?

No, mf is just another reference to the same object held in the list. So if
you change the properties of mf, you will also see those changes in the
properties of the object in the list; as they are one and the same object.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
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
Change the font size for the list(data validation) content ViestaWu Microsoft Excel Worksheet Functions 1 31st Oct 2008 11:57 AM
How can I change list box's content via other list box's selected item? Ruzanna Microsoft Access Macros 1 22nd Feb 2004 01:02 PM
How can I change list box's content via other list box's selected item? Ruzanna Microsoft Access Form Coding 1 22nd Feb 2004 12:31 PM
How can I change list box's content via other list box's selected item? Ruzanna Microsoft Access Forms 0 21st Feb 2004 12:23 PM
How can I change list box's content via other list box's selected item? Ruzanna Microsoft Access Macros 0 21st Feb 2004 11:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:46 PM.