Solved

c# 2010 winforms list of classes problem

Posted on 2013-12-16
7
196 Views
Last Modified: 2013-12-16
Hi

I have a class, like this:

class MyClass
{

public int ID {get; set;}
public string[] Stuff = new string[10];

public MyClass(int ID, string[] Stuff)
{

this.ID = ID;
this.Stuff = Stuff;


}

}

Open in new window


simple enough...

I have a list of this class, which I am adding to like so

List<MyClass> myStuff = new List<MyClass>();

myStuff.add(new MyClass(id,stuff));

Open in new window


this is fine when I add the first entry, but for subsequent entries, the 'stuff' array is being applied to ALL entries in the list, not just the new one.

I am baffled as to why, I am sure someone can enlighten me.


cheers

CD
0
Comment
Question by:cycledude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39721140
You mean you are passing the same instance of "stuff" to each MyClass object? What you are actually passing is a pointer to the array, not a copy.

You should look at creating a copy of the array inside each MyClass instance if you want them to remain separate.
0
 

Author Comment

by:cycledude
ID: 39721148
well, its the same array structure as in

string[] stuff = new string[10]

but the contents of the string are different on each iteration
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39721159
But are you overwriting the contents of the array in each iteration, or are you reinitializing the array each time.

i.e. Are you doing:
string[] stuff = new string[10];

for ( ... )
{
    stuff[0] = "something";
    stuff[1] = "something else";
    ...
}

Open in new window

Or are you doing:
string[] stuff;

for ( ... )
{
    stuff = new string[10];
    stuff[0] = "something";
    stuff[1] = "something else";
    ...
}

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cycledude
ID: 39721163
1st one ...
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39721166
In that case, as I said originally, you are passing a reference to the same array to each instance, so any changes you make get made to all references.

Use the second version to create a new array for each instance and you should be ok.
0
 

Author Comment

by:cycledude
ID: 39721171
Hi

thanks,  I will try to re-structure the code so that I can re-initialise the array on each iteration...

will let you know how i get on.
0
 

Author Closing Comment

by:cycledude
ID: 39721260
thanks, a little restructuring and it has worked a treat.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question