473,795 Members | 2,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change content of a List<T>?

I am using List<MyClassto 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 = GetMyClassIndFr omName(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,
Aug 31 '06 #1
5 2007
Gary,
Let's clear this up with a handy-dandy little console app you can run:

using System;
using System.Collecti ons.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<MyClassmyC lasses= new List<MyClass>() ;
myClasses.Add(n ew MyClass(1, "Testing1") );
myClasses.Add(n ew MyClass(2, "Testing2") );
MyClass test = myClasses[0];
test.Name = "BORK!";
test.Id = 1229;
MyClass test2 = myClasses[0];
Console.WriteLi ne(test2.Id.ToS tring() + ": " + test2.Name);
Console.ReadLin e();
}
}
}

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


"Gary" wrote:
I am using List<MyClassto 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 = GetMyClassIndFr omName(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,
Aug 31 '06 #2
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" <Ga**@discussio ns.microsoft.co mwrote in message
news:BE******** *************** ***********@mic rosoft.com...
>I am using List<MyClassto 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 = GetMyClassIndFr omName(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,

Aug 31 '06 #3
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" <Ga**@discussio ns.microsoft.co mwrote in message
news:BE******** *************** ***********@mic rosoft.com...
I am using List<MyClassto 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 = GetMyClassIndFr omName(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,


Sep 1 '06 #4
Yes, value types are always copied.
--
HTH
Stoitcho Goutsev (100)

"Gary" <Ga**@discussio ns.microsoft.co mwrote in message
news:CD******** *************** ***********@mic rosoft.com...
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" <Ga**@discussio ns.microsoft.co mwrote in message
news:BE******* *************** ************@mi crosoft.com...
>I am using List<MyClassto 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 = GetMyClassIndFr omName(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,



Sep 5 '06 #5
"Gary" <Ga**@discussio ns.microsoft.co ma écrit dans le message de news:
BE************* *************** **...icrosof t.com...

|I am using List<MyClassto 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 = GetMyClassIndFr omName(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
Sep 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5635
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
4
53016
by: matty.hall | last post by:
I have two classes: a base class (BaseClass) and a class deriving from it (DerivedClass). I have a List<DerivedClass> that for various reasons needs to be of that type, and not a List<BaseClass>. However, I need to cast that list to a List<BaseClass> and it is not working. The code is below. I get the following exception: "Unable to cast object of type 'System.Collections.Generic.List`1' to type 'System.Collections.Generic.List`1'." ...
2
2683
by: Brian Pelton | last post by:
I am not sure how to fix this problem I've stumbled into... I have a list<> of an interface type. I need to pass that list to a method that adds more objects to the list. But, eventually, I need to get back to the original list<> object. --- In other words; let's say I start with an List<> of 3 objects. I call a
3
2102
by: Varangian | last post by:
Hello, there I have a problem with regards to System.Collections.Generic.List<T> I need to pass a class with implements an interface - TestClass : IPerson I put this class in a List<TestClass> = new List<TestClass>(); then I pass this List<TestClass> to a function which takes an argument List<IPerson> person
9
7910
by: Paul | last post by:
Hi, I feel I'm going around circles on this one and would appreciate some other points of view. From a design / encapsulation point of view, what's the best practise for returning a private List<as a property. Consider the example below, the class "ListTest" contains a private "List<>" called "strings" - it also provides a public method to add to that list,
0
1753
by: Iron Moped | last post by:
I'm airing frustration here, but why does LinkedList<not support the same sort and search methods as List<>? I want a container that does not support random access, allows forward and reverse traversal and natively supports sorting, i.e., STL's list<T>. There isn't even a set of algorithms that would allow me to easily sort a generic collection. System.Array has a robust set of static algorithms, why not extend this to ICollection?
7
57556
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list without itterating through the entire collection and building up a list? 2. is there a common base class, collection or interface that can contain either/both of these collection types and then how do you convert or cast from the base to either a...
56
5208
by: Zytan | last post by:
Obviously you can't just use a simple for loop, since you may skip over elements. You could modify the loop counter each time an element is deleted. But, the loop ending condition must be checked on each iteration, since the Count changes as you delete elements. I would think it is guaranteed to be computed each time, and not cached. So, is this the best way?
4
10613
by: Peted | last post by:
I have the following code public enum pdfFlags { PFD_DRAW_TO_WINDOW, PFD_DRAW_TO_BITMAP, PFD_SUPPORT_GDI, PFD_SUPPORT_OPENGL, PFD_GENERIC_ACCELERATED, PFD_GENERIC_FORMAT,
35
5895
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the list. That part makes a reasonable amount of sense, as you can't actually take items away from an Array. However, there doesn't seem to be a way to perform the same operation in one fell swoop on a List<>. For example:
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7541
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.