473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Copy Constructor to load List<T>

In VS2003 I have been using an ArrayList loading it with a copy constructor
and it seems to work fine. {I am acutally loading a lot of constants first,
not one a shown}

public ArrayList al;
....
MyClass mc = new MyClass()
mc.a = 1;
loop...
{
mc.b = loopcount;
al.Add((object)(new MyClass(mc)));
}

With VS2005 I decided to change it to to generics List, but this made all of
the items in the list the same (takes on the last MyClass loaded values).
public List<MyClass> al;
....
MyClass mc = new MyClass()
mc.a = 1;
loop...
{
mc.b = loopcount;
al.Add(new MyClass(mc));
}
So I tweeked it to create a new mc each time in the loop which works, but it
would seem the other should work too.
public List<MyClass> al;
....
loop...
{
MyClass mc = new MyClass()
mc.a = 1;
mc.b = loopcount;
al.Add(new MyClass(mc));
}
It seems the new and copy constructor isn't making a new object. As I am a
copy constructor junkie this worries me.
Nov 18 '05 #1
1 2787
GRiN,

The second loop is excessive. Since you are creating a new instance
every time, there is no need to use the extra copy constructor.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"GRiN" <GR**@discussions.microsoft.com> wrote in message
news:6B**********************************@microsof t.com...
In VS2003 I have been using an ArrayList loading it with a copy
constructor
and it seems to work fine. {I am acutally loading a lot of constants
first,
not one a shown}

public ArrayList al;
...
MyClass mc = new MyClass()
mc.a = 1;
loop...
{
mc.b = loopcount;
al.Add((object)(new MyClass(mc)));
}

With VS2005 I decided to change it to to generics List, but this made all
of
the items in the list the same (takes on the last MyClass loaded values).
public List<MyClass> al;
...
MyClass mc = new MyClass()
mc.a = 1;
loop...
{
mc.b = loopcount;
al.Add(new MyClass(mc));
}
So I tweeked it to create a new mc each time in the loop which works, but
it
would seem the other should work too.
public List<MyClass> al;
...
loop...
{
MyClass mc = new MyClass()
mc.a = 1;
mc.b = loopcount;
al.Add(new MyClass(mc));
}
It seems the new and copy constructor isn't making a new object. As I am
a
copy constructor junkie this worries me.

Nov 18 '05 #2

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

Similar topics

14
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...
4
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>....
2
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...
5
by: PJ | last post by:
I have a class definition : public class PagingList<T> : List<T> { private int pageSize, pageNumber; public PagingList() { pageSize = (this.Count == 0) ? 1 : this.Count;...
3
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...
9
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...
0
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...
7
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...
45
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies...
4
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,
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.