Change content of a List<T>?

  • Thread starter Thread starter Guest
  • Start date Start date
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,
 
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
 
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?
 
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,
 
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,
 
"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
 
Back
Top