Collection/list problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Clive Lumb

    Collection/list problem

    Hi,

    I'm having a problem while adding objects to a collection. (VB.net 2005)
    I am reading data in from a file to a temporary variable (eg MyClass) and
    then adding it to a collection.
    When I then enumerate the collection, all the entries are equal to the last
    one added.
    The same thing happens with lists and with a generic list(of type).
    Obviously the collection or list is storing a reference to the variable,
    rather than the values - hence all the items refer to the same variable.

    I am probably missing something really basic - please be patient :-)

    Many TIA

    example:

    Public Class MyThing
    name as String
    Value as integer
    end class

    Dim MyClass as New MyThing
    Dim MyCollection as New Collection

    'open file
    'do
    ' read in name, value
    MyClass.name=Na meFromFile
    MyClass.value=V alueFromFile
    MyCollection.Ad d(MyClass)
    'loop to end of file
    'close file



  • Patrice

    #2
    Re: Collection/list problem

    You need to add MyClass=New MyThing inside the loop.

    Here you always adding the same object to the collection. Even once the
    object is added to the collection you can change the object properties
    (basically an object is a pointer actual data).

    I would recommend reading once the language specification to clear out the
    basics...

    --
    Patrice

    "Clive Lumb" <clumb2@gratuit _en_anglais.fr. invalida écrit dans le message
    de groupe de discussion : 488839a0$0$2131 2$426a74cc@news .free.fr...
    Hi,
    >
    I'm having a problem while adding objects to a collection. (VB.net 2005)
    I am reading data in from a file to a temporary variable (eg MyClass) and
    then adding it to a collection.
    When I then enumerate the collection, all the entries are equal to the
    last
    one added.
    The same thing happens with lists and with a generic list(of type).
    Obviously the collection or list is storing a reference to the variable,
    rather than the values - hence all the items refer to the same variable.
    >
    I am probably missing something really basic - please be patient :-)
    >
    Many TIA
    >
    example:
    >
    Public Class MyThing
    name as String
    Value as integer
    end class
    >
    Dim MyClass as New MyThing
    Dim MyCollection as New Collection
    >
    'open file
    'do
    ' read in name, value
    MyClass.name=Na meFromFile
    MyClass.value=V alueFromFile
    MyCollection.Ad d(MyClass)
    'loop to end of file
    'close file
    >
    >
    >

    Comment

    • Clive Lumb

      #3
      Re: Collection/list problem

      Hello Patrice

      Thanks for the speedy reply!
      I shall try that immediately

      "Patrice" <http://www.chez.com/scribe/a écrit dans le message de news:
      7A8AF0C2-88AC-4B3D-9FBC-24835CAF46F3@mi crosoft.com...
      You need to add MyClass=New MyThing inside the loop.
      >
      Here you always adding the same object to the collection. Even once the
      object is added to the collection you can change the object properties
      (basically an object is a pointer actual data).
      >
      I would recommend reading once the language specification to clear out the
      basics...
      >
      --
      Patrice
      >
      "Clive Lumb" <clumb2@gratuit _en_anglais.fr. invalida écrit dans le
      message de groupe de discussion :
      488839a0$0$2131 2$426a74cc@news .free.fr...
      >Hi,
      >>
      >I'm having a problem while adding objects to a collection. (VB.net 2005)
      >I am reading data in from a file to a temporary variable (eg MyClass) and
      >then adding it to a collection.
      >When I then enumerate the collection, all the entries are equal to the
      >last
      >one added.
      >The same thing happens with lists and with a generic list(of type).
      >Obviously the collection or list is storing a reference to the variable,
      >rather than the values - hence all the items refer to the same variable.
      >>
      >I am probably missing something really basic - please be patient :-)
      >>
      >Many TIA
      >>
      >example:
      >>
      >Public Class MyThing
      > name as String
      > Value as integer
      >end class
      >>
      >Dim MyClass as New MyThing
      >Dim MyCollection as New Collection
      >>
      >'open file
      >'do
      > ' read in name, value
      > MyClass.name=Na meFromFile
      > MyClass.value=V alueFromFile
      > MyCollection.Ad d(MyClass)
      >'loop to end of file
      >'close file
      >>
      >>
      >>
      >

      Comment

      • Clive Lumb

        #4
        Re: Collection/list problem

        Yay! it works!
        I would recommend reading once the language specification to clear out the
        basics...
        >
        Unfortunately I have the French version which is dire, for example it says
        that objects in a collection *should* not be of the same type (instead of
        *need* not).



        "Patrice" <http://www.chez.com/scribe/a écrit dans le message de news:
        7A8AF0C2-88AC-4B3D-9FBC-24835CAF46F3@mi crosoft.com...
        You need to add MyClass=New MyThing inside the loop.
        >
        Here you always adding the same object to the collection. Even once the
        object is added to the collection you can change the object properties
        (basically an object is a pointer actual data).
        >
        I would recommend reading once the language specification to clear out the
        basics...
        >
        --
        Patrice
        >
        "Clive Lumb" <clumb2@gratuit _en_anglais.fr. invalida écrit dans le
        message de groupe de discussion :
        488839a0$0$2131 2$426a74cc@news .free.fr...
        >Hi,
        >>
        >I'm having a problem while adding objects to a collection. (VB.net 2005)
        >I am reading data in from a file to a temporary variable (eg MyClass) and
        >then adding it to a collection.
        >When I then enumerate the collection, all the entries are equal to the
        >last
        >one added.
        >The same thing happens with lists and with a generic list(of type).
        >Obviously the collection or list is storing a reference to the variable,
        >rather than the values - hence all the items refer to the same variable.
        >>
        >I am probably missing something really basic - please be patient :-)
        >>
        >Many TIA
        >>
        >example:
        >>
        >Public Class MyThing
        > name as String
        > Value as integer
        >end class
        >>
        >Dim MyClass as New MyThing
        >Dim MyCollection as New Collection
        >>
        >'open file
        >'do
        > ' read in name, value
        > MyClass.name=Na meFromFile
        > MyClass.value=V alueFromFile
        > MyCollection.Ad d(MyClass)
        >'loop to end of file
        >'close file
        >>
        >>
        >>
        >

        Comment

        • Lev Blaivas

          #5
          Re: Collection/list problem

          On Jul 24, 11:54 am, "Clive Lumb"
          <clumb2@gratuit _en_anglais.fr. invalidwrote:
          Yay! it works!
          >
          I would recommend reading once the language specification to clear out the
          basics...
          >
          Unfortunately I have the French version which is dire, for example it says
          that objects in a collection *should* not be of the same type (instead of
          *need* not).
          >
          "Patrice" <http://www.chez.com/scribe/a écrit dans le message de news:
          7A8AF0C2-88AC-4B3D-9FBC-24835CAF4...@mi crosoft.com...
          >
          You need to add MyClass=New MyThing inside the loop.
          >
          Here you always adding the same object to the collection. Even once the
          object is added to the collection you can change the object properties
          (basically an object is a pointer actual data).
          >
          I would recommend reading once the language specification to clear out the
          basics...
          >
          --
          Patrice
          >
          "Clive Lumb" <clumb2@gratuit _en_anglais.fr. invalida écrit dans le
          message de groupe de discussion :
          488839a0$0$2131 2$426a7...@news .free.fr...
          Hi,
          >
          I'm having a problem while adding objects to a collection. (VB.net 2005)
          I am reading data in from a file to a temporary variable (eg MyClass) and
          then adding it to a collection.
          When I then enumerate the collection, all the entries are equal to the
          last
          one added.
          The same thing happens with lists and with a generic list(of  type).
          Obviously the collection or list is storing a reference to the variable,
          rather than the values - hence all the items refer to the same variable.
          >
          I am probably missing something really basic - please be patient :-)
          >
          Many TIA
          >
          example:
          >
          Public Class MyThing
             name as String
             Value as integer
          end class
          >
          Dim MyClass as New MyThing
          Dim MyCollection as New Collection
          >
          'open file
          'do
             ' read in name, value
             MyClass.name=Na meFromFile
             MyClass.value=V alueFromFile
             MyCollection.Ad d(MyClass)
          'loop to end of file
          'close file
          Here is a great article about reference and value types;
          Now, next, and beyond: Tracking need-to-know trends at the intersection of business and technology

          explains in detail the difference between value and reference types

          Comment

          Working...