Re: Collection/list problem



On Jul 24, 11:54 am, "Clive Lumb"
<clumb2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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...@xxxxxxxxxxxxxxxx

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@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le
message de groupe de discussion :
488839a0$0$21312$426a7...@xxxxxxxxxxxxxxx
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=NameFromFile
   MyClass.value=ValueFromFile
   MyCollection.Add(MyClass)
'loop to end of file
'close file

Here is a great article about reference and value types;
http://www.ondotnet.com/lpt/a/2789
explains in detail the difference between value and reference types
.



Relevant Pages

  • Re: Maintain list of attached event handlers (.Net 1.1)
    ... Your requirement to unsubscribe when the event fires is completely independent of how you unsubscribe other events later. ... A basic rule of event management is that when you subscribe the first handler, the reference to the event has to be instantiated. ... While you didn't actually post any code that showed such an enumeration, I will take as granted that somewhere you actually do. ... It's simple, it works, and is MORE performant than trying to maintain a list or lists or other data structures as various events are raised and unsubscribed from. ...
    (microsoft.public.dotnet.framework)
  • Re: Call by value, but (please check my hypothesis)
    ... objects are passed by reference in Lisp. ... > to lists ... SETF actually understands the syntax of the (GETF ...
    (comp.lang.lisp)
  • Re: [PATCH 2.6.23.14] SCSI : scsi_device_lookup/scsi_device_lookup_by_target return NULL for an exis
    ... because there is some outstanding command holding a reference to it. ... scanning will go ahead and try to add a new scsi_device to the devices lists. ... the consensus was to resurrect the sdev from the SDEV_DEL state back ... - that patches for the resurrection where implemented post this thread ...
    (Linux-Kernel)
  • Re: time dilation
    ... the laws of physics must remain the same in all frames of reference. ... train car to the floor, it falls a distance of y'. ...   The ... frame of reference. ...
    (sci.physics.relativity)
  • Re: Complex Subset Computing in Prolog
    ...    variables is unknown build something like distinct. ... There is often confusion when lists and sets are used (in Prolog or ... but let's do this for Prolog) and this pops up ... list representation that is not unique. ...
    (comp.lang.prolog)