Probably missing something simple

P

Paul

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<Links> linksStructFollowup= 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
 
G

Göran Andersson

Paul said:
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<Links> linksStructFollowup= 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
 
F

fd123456

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
 
P

Paul

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.


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


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<Links> linksStructFollowup= 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
 
P

Paul

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 said:
Paul said:
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<Links> linksStructFollowup= 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top