Solved

c# 2010 winforms list of classes problem

Posted on 2013-12-16
7
193 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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET(C#) Eliminating weekends from a date range 2 54
Please explain the parts of these 2 LINQ expressions 3 83
Why use this lambda? 12 62
aspx ascx, c# 7 9
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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