Change content of a List<T>?

G

Guest

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,
 
G

Guest

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
 
S

Stoitcho Goutsev \(100\)

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?
 
G

Guest

Yes. It is a structure actually. I believe that caused the problem.

Thanks,

Stoitcho Goutsev (100) said:
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 said:
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,
 
S

Stoitcho Goutsev \(100\)

Yes, value types are always copied.


--
HTH
Stoitcho Goutsev (100)

Gary said:
Yes. It is a structure actually. I believe that caused the problem.

Thanks,

Stoitcho Goutsev (100) said:
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 said:
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,
 
J

Joanna Carter [TeamB]

"Gary" <[email protected]> a écrit dans le message de (e-mail address 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
 

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