Probably missing something simple

  • Thread starter Thread starter Paul
  • Start date Start date
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
 
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
 
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 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
 
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
 
Back
Top