Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Probably missing something simple

Reply
Thread Tools

Probably missing something simple

 
 
Paul
Guest
Posts: n/a
 
      06-20-2008
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.
 
Reply With Quote
 
 
 
 
Göran Andersson
Guest
Posts: n/a
 
      06-21-2008
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
 
Reply With Quote
 
 
 
 
fd123456@hotmail.com
Guest
Posts: n/a
 
      06-22-2008
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 <(E-Mail Removed)> 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.


 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      06-23-2008
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.


"(E-Mail Removed)" 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 <(E-Mail Removed)> 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.

>
>

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      06-23-2008
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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: ValueError: Procedure probably called with not enough arguments(8 bytes missing) LabJack Support Python 0 08-07-2009 10:17 PM
ValueError: Procedure probably called with not enough arguments (8bytes missing) LabJack Support Python 0 08-07-2009 10:16 PM
[Newbie Alert] Probably missing something simple here ... help {wimper} B. Angell Ruby 4 08-16-2005 09:47 PM
Re: I'm missing something simple Kevin Spencer ASP .Net 1 05-20-2004 04:56 PM
Distutils confusion - probably something simple python newbie Python 0 05-09-2004 02:16 PM



Advertisments