Stupid question about List(Of T)

  • Thread starter Thread starter Clive Lumb
  • Start date Start date
C

Clive Lumb

Hi,

Being of advancing age and little brain, I can't for the life of me remember
how to add a class to a list without it being simply a reference.

Pseudo code:

MyClass
This Property, ThatProperty
End Class

MainClass
Dim MyList as New List(Of MyClass)

dim TempC as new MyClass
For each set of lines in a file
Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty
MyList.Add(Temp)
Next

MyList ends up containing the correct number of items, but every single one
is the same as the last TempC added.


There must be something glaringly obvious.
Please make me say "D'oh!"

Cheers
 
Clive Lumb said:
Hi,

Being of advancing age and little brain, I can't for the life of me
remember how to add a class to a list without it being simply a reference.

Pseudo code:

MyClass
This Property, ThatProperty
End Class

MainClass
Dim MyList as New List(Of MyClass)

dim TempC as new MyClass
For each set of lines in a file
Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty
MyList.Add(Temp)
Next

MyList ends up containing the correct number of items, but every single
one is the same as the last TempC added.


There must be something glaringly obvious.
Please make me say "D'oh!"

Cheers
I told you it was stupid!!!

Just realised that I needed to do a
Temp= New MyClass
after adding to the list.

D'oh !
 
Brian Cryer said:
Probably better to create a new one BEFORE rather than after adding, so:

For each set of lines in a file
dim TempC as new MyClass
Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty
MyList.Add(Temp)
Next

if you do it after then you end up with an instance of your class that you
never use. No big deal in the overall sheme of things.
How about:

For each set of lines in a file
Using TempC as new MyClass
Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty
MyList.Add(Temp)
End Using
Next

However, MyClass does need to implement iDisposable
 
How about:

For each set of lines in a file
Using TempC as new MyClass
Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty
MyList.Add(Temp)
End Using
Next

However, MyClass does need to implement iDisposable

Assuming you meant:
MyList.Add(Tempc)

you are disposing each MyClass instance after you add it to the list.
That won't work.
 
Jack Jackson said:
Assuming you meant:
MyList.Add(Tempc)

you are disposing each MyClass instance after you add it to the list.
That won't work.
Sorry about the Temp, I did a copy paste from the code above. The above
does, in fact, work, providing you leave the iDisposable implementation
methods empty, but I do take your point.
 
Harry said:
Sorry about the Temp, I did a copy paste from the code above. The above
does, in fact, work, providing you leave the iDisposable implementation
methods empty, but I do take your point.

Well, it "works" because you implement IDisposable to not work. It's a
flawed design that relies on another part of the design to also be
flawed to work. It's a maintenance nightmare...
 
Back
Top