Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Probably missing something simple (http://www.velocityreviews.com/forums/t621433-probably-missing-something-simple.html)

Paul 06-20-2008 10:53 PM

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<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 G
Software engineer.

Göran Andersson 06-21-2008 12:45 AM

Re: Probably missing something simple
 
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<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
>



--
Göran Andersson
_____
http://www.guffa.com

fd123456@hotmail.com 06-22-2008 12:24 PM

Re: Probably missing something simple
 
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.com> 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<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 G
> Software engineer.



Paul 06-23-2008 02:47 PM

Re: Probably missing something simple
 
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.


"fd123456@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.com> 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<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 G
> > Software engineer.

>
>


Paul 06-23-2008 02:48 PM

Re: Probably missing something simple
 
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<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
> >

>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com
>



All times are GMT. The time now is 12:32 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.