Probably missing something simple

Discussion in 'ASP .Net' started by Paul, Jun 20, 2008.

  1. Paul

    Paul Guest

    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, Jun 20, 2008
    #1
    1. Advertising

  2. 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
     
    Göran Andersson, Jun 21, 2008
    #2
    1. Advertising

  3. Paul

    Guest

    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 <> 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.
     
    , Jun 22, 2008
    #3
  4. Paul

    Paul Guest

    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.


    "" 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 <> 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, Jun 23, 2008
    #4
  5. Paul

    Paul Guest

    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
    >
     
    Paul, Jun 23, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: I'm missing something simple

    Kevin Spencer, May 20, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    327
    =?Utf-8?B?Y2N1cnZleQ==?=
    May 20, 2004
  2. python newbie
    Replies:
    0
    Views:
    314
    python newbie
    May 9, 2004
  3. LabJack Support
    Replies:
    0
    Views:
    519
    LabJack Support
    Aug 7, 2009
  4. LabJack Support
    Replies:
    0
    Views:
    375
    LabJack Support
    Aug 7, 2009
  5. B. Angell
    Replies:
    4
    Views:
    93
    William James
    Aug 16, 2005
Loading...

Share This Page