473,406 Members | 2,707 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,406 software developers and data experts.

Probably missing something simple

Hi I have a generic list with the type being an object and am trying to fill
the list. It works partially but keeps overwritting the previous values. I
have
the generic list of objects of type Links
public List<LinkslinksStructFollowup= new List<Links();
instance of an object of type Links
public Links newstructRelatedFollowup = new Links();
Links is just a class containing an integer and two strings
public class Links
{

private int _CountLink;
private string _URLLink;
private string _DescriptionLink;
}
along with the constructor to allow outside access to data and a few methods.

I have a loop as shown
for (int i = 0; i < 3; i++)
{
newstructRelatedFollowup.CountLink = i;
linksStructFollowup.Add(newstructRelatedFollowup);
}
Although a new object is added to the list, the linksStructFollowup data
of the previous itteration gets over written so you end up with
linkStructFollowup[0].CountLink = 2
linkStructFollowup[1].CountLink = 2
linkStructFollowup[2].CountLink = 2
where it should be
linkStructFollowup[0].CountLink = 0
linkStructFollowup[1].CountLink = 1
linkStructFollowup[2].CountLink = 2

--
Paul G
Software engineer.
Jun 27 '08 #1
4 1067
Paul wrote:
Hi I have a generic list with the type being an object and am trying to fill
the list. It works partially but keeps overwritting the previous values. I
have
the generic list of objects of type Links
public List<LinkslinksStructFollowup= new List<Links();
instance of an object of type Links
public Links newstructRelatedFollowup = new Links();
Links is just a class containing an integer and two strings
public class Links
{

private int _CountLink;
private string _URLLink;
private string _DescriptionLink;
}
along with the constructor to allow outside access to data and a few methods.

I have a loop as shown
for (int i = 0; i < 3; i++)
{
newstructRelatedFollowup.CountLink = i;
linksStructFollowup.Add(newstructRelatedFollowup);
}
Although a new object is added to the list,
It's not. You are just changing the data in the same object, and adding
the same object to the list over and over again. You end up with a list
where every item references the same object.

Adding an object to the list doesn't create a copy of the object, it
only adds the reference to the object.

You have to create a new instance of the class for every iteration, so
that you have a new object to add to the list each time.
the linksStructFollowup data
of the previous itteration gets over written so you end up with
linkStructFollowup[0].CountLink = 2
linkStructFollowup[1].CountLink = 2
linkStructFollowup[2].CountLink = 2
where it should be
linkStructFollowup[0].CountLink = 0
linkStructFollowup[1].CountLink = 1
linkStructFollowup[2].CountLink = 2

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
Hi Paul,

Turn the class Links into a struct. Struct are supposed to hold
values, while classes should hold behavior. Here, adding a struct to
the List would add a copy of that struct, much like if you had added
an int or a long or any integral type. Instead, as Links is a class (a
reference type), you're adding the reference and not the values
themselves.

HTH,

Michel
On 21 juin, 00:53, Paul <P...@discussions.microsoft.comwrote:
Hi I have a generic list with the type being an object and am trying to fill
the list. *It works partially but keeps overwritting the previous values.. *I
have
the generic list of objects of type Links
public List<LinkslinksStructFollowup= new List<Links();
instance of an object of type Links
public Links newstructRelatedFollowup = new Links();
*Links is just a class containing an integer and two strings
public class Links
* * {

* * * * private int _CountLink;
* * * * private string _URLLink;
* * * * private string _DescriptionLink;}

along with the constructor to allow outside access to data and a few methods.

I have a loop as shown
* *for (int i = 0; i < 3; i++)
* * * * * * * * * * * * {
* * * * * * * * *newstructRelatedFollowup.CountLink = i;
* * * * * * * * linksStructFollowup.Add(newstructRelatedFollowup); *
* * * * * * * * * * * * } * *
Although a new object is added to the list, the linksStructFollowup data
*of the previous itteration gets over written so you end up with
linkStructFollowup[0].CountLink = 2
linkStructFollowup[1].CountLink = 2
linkStructFollowup[2].CountLink = 2
where it should be
linkStructFollowup[0].CountLink = 0
linkStructFollowup[1].CountLink = 1
linkStructFollowup[2].CountLink = 2

--
Paul G
Software engineer.
Jun 27 '08 #3
Hi thanks for the response. I originally had the links as a struct but then
had problems when I tried to bind it to a gridview so ended up converting it
to an object. I think the gridview object is looking for properties to bind
to, although not sure.

--
Paul G
Software engineer.
"fd******@hotmail.com" wrote:
Hi Paul,

Turn the class Links into a struct. Struct are supposed to hold
values, while classes should hold behavior. Here, adding a struct to
the List would add a copy of that struct, much like if you had added
an int or a long or any integral type. Instead, as Links is a class (a
reference type), you're adding the reference and not the values
themselves.

HTH,

Michel
On 21 juin, 00:53, Paul <P...@discussions.microsoft.comwrote:
Hi I have a generic list with the type being an object and am trying to fill
the list. It works partially but keeps overwritting the previous values.. I
have
the generic list of objects of type Links
public List<LinkslinksStructFollowup= new List<Links();
instance of an object of type Links
public Links newstructRelatedFollowup = new Links();
Links is just a class containing an integer and two strings
public class Links
{

private int _CountLink;
private string _URLLink;
private string _DescriptionLink;}

along with the constructor to allow outside access to data and a few methods.

I have a loop as shown
for (int i = 0; i < 3; i++)
{
newstructRelatedFollowup.CountLink = i;
linksStructFollowup.Add(newstructRelatedFollowup);
}
Although a new object is added to the list, the linksStructFollowup data
of the previous itteration gets over written so you end up with
linkStructFollowup[0].CountLink = 2
linkStructFollowup[1].CountLink = 2
linkStructFollowup[2].CountLink = 2
where it should be
linkStructFollowup[0].CountLink = 0
linkStructFollowup[1].CountLink = 1
linkStructFollowup[2].CountLink = 2

--
Paul G
Software engineer.

Jun 27 '08 #4
Hi thanks for the response, will give it a try. I was thinking it acts the
same as a list of structures but that is not the case~
--
Paul G
Software engineer.
"Göran Andersson" wrote:
Paul wrote:
Hi I have a generic list with the type being an object and am trying to fill
the list. It works partially but keeps overwritting the previous values. I
have
the generic list of objects of type Links
public List<LinkslinksStructFollowup= new List<Links();
instance of an object of type Links
public Links newstructRelatedFollowup = new Links();
Links is just a class containing an integer and two strings
public class Links
{

private int _CountLink;
private string _URLLink;
private string _DescriptionLink;
}
along with the constructor to allow outside access to data and a few methods.

I have a loop as shown
for (int i = 0; i < 3; i++)
{
newstructRelatedFollowup.CountLink = i;
linksStructFollowup.Add(newstructRelatedFollowup);
}
Although a new object is added to the list,

It's not. You are just changing the data in the same object, and adding
the same object to the list over and over again. You end up with a list
where every item references the same object.

Adding an object to the list doesn't create a copy of the object, it
only adds the reference to the object.

You have to create a new instance of the class for every iteration, so
that you have a new object to add to the list each time.
the linksStructFollowup data
of the previous itteration gets over written so you end up with
linkStructFollowup[0].CountLink = 2
linkStructFollowup[1].CountLink = 2
linkStructFollowup[2].CountLink = 2
where it should be
linkStructFollowup[0].CountLink = 0
linkStructFollowup[1].CountLink = 1
linkStructFollowup[2].CountLink = 2


--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #5

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

Similar topics

0
by: python newbie | last post by:
Ok, I am struggling with this setup.py. My python script happens to use the mxBase library, specifically the mx.DateTime package. That's the only "third party" package I use. I've gone through...
7
by: LeROY | last post by:
I need to output some related data tables as XML. I have my data adapters and my dataset with the relations defined. It is certainly simple enough to use the dataset.WriteXML function. However,...
1
by: Robert Muchacki | last post by:
Hello all! I'm fighting with this little problem. I'm trying to retrieve data from a table in SQL Server 2k. I'm dead. The only result I get is retrieving the headers of the columns - no data in...
102
by: Skybuck Flying | last post by:
Sometime ago on the comp.lang.c, I saw a teacher's post asking why C compilers produce so many error messages as soon as a closing bracket is missing. The response was simply because the compiler...
2
by: Andrew | last post by:
Hey all, Have a strange one here, and being still fairly new to .NET isn't helping me understand it. I am having a problem where a DataReader doesn't return all the rows when I try to use a...
5
by: unwantedspam | last post by:
Hi All, What am I missing. I have trying to create a simple web based application. Basically there are three textboxes and two buttons (Save, Reset). I assume the code behind the save button...
5
by: matthewtec | last post by:
This may be something that is painfully easy and I'm just missing, or it could be something that I should learn a bit more in order to do. But, if I have a simple windows form application, with...
0
by: Jigar.Patel | last post by:
I have simple remoting server exposing following simple method. When I try to add webreference to this server in another project, it gives me following error: Custom tool error: Unable to import...
12
by: The Frog | last post by:
Hi all, Does anyone have a way to print forms at design time directly from the IDE? I am not a great fan of the print screen to paint to print method. I would really like to know if anyone has a...
1
by: paulf.johnson | last post by:
Hi, For some reason, my brain has gone to mush on a couple of things. Any help would be appreciated. I have an array and a field passed into a function (call it $v) which is a string composed...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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,...
0
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...

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.