Solved

c# 2010 winforms list of classes problem

Posted on 2013-12-16
7
191 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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