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=NameFromFile
MyClass.value=ValueFromFile
MyCollection.Add(MyClass)
'loop to end of file
'close file



.



Relevant Pages

  • Re: Collection/list problem
    ... I would recommend reading once the language specification to clear out the ... The same thing happens with lists and with a generic list. ... Public Class MyThing ... Dim MyClass as New MyThing ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Collection/list problem
    ... I would recommend reading once the language specification to clear out the ... The same thing happens with lists and with a generic list. ... Public Class MyThing ... Dim MyClass as New MyThing ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Dataset to Generic.List(Of MyClass)
    ... Dim id As DataColumn = table.Columns ... Dim name As DataColumn = table.Columns ... Dim list As New List(Of MyClass) ... all you're doing is reading data and generating lists of classes, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Generics, extending LinkedList, and unchecked casts
    ... Mark Maloof wrote: ... Lists are ordered already. ... but I cannot figure out how to avoid the unchecked cast ... MyClass into your LinkedList. ...
    (comp.lang.java.programmer)
  • Re: Dataset to Generic.List(Of MyClass)
    ... Dim id As DataColumn = table.Columns ... Dim name As DataColumn = table.Columns ... Dim list As New List(Of MyClass) ... all you're doing is reading data and generating lists of classes, ...
    (microsoft.public.dotnet.framework.aspnet)

Loading