Re: Stupid question about List(Of T)
- From: Jack Jackson <jjackson-nsp@xxxxxxxxxxxxxxxx>
- Date: Tue, 22 Sep 2009 13:54:08 -0700
On Wed, 23 Sep 2009 05:57:01 +1000, "Harry Strybos"
<nospam@xxxxxxxxxxxxxxxxxx> wrote:
"Brian Cryer" <not.here@localhost> wrote in message
news:ezLby35OKHA.4244@xxxxxxxxxxxxxxxxxxxxxxx
"Clive Lumb" <clumb2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in messageHow about:
news:%23uZ6Iq4OKHA.504@xxxxxxxxxxxxxxxxxxxxxxx
"Clive Lumb" <clumb2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le
message de news: ufKXki4OKHA.4004@xxxxxxxxxxxxxxxxxxxxxxx
Hi,I told you it was stupid!!!
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
Just realised that I needed to do a
Temp= New MyClass
after adding to the list.
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.
--
Brian Cryer
www.cryer.co.uk/brian
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.
.
- Follow-Ups:
- Re: Stupid question about List(Of T)
- From: Harry Strybos
- Re: Stupid question about List(Of T)
- References:
- Stupid question about List(Of T)
- From: Clive Lumb
- Re: Stupid question about List(Of T)
- From: Clive Lumb
- Re: Stupid question about List(Of T)
- From: Brian Cryer
- Re: Stupid question about List(Of T)
- From: Harry Strybos
- Stupid question about List(Of T)
- Prev by Date: Re: Stupid question about List(Of T)
- Next by Date: RE: Winzip or Compressed folders from VB
- Previous by thread: Re: Stupid question about List(Of T)
- Next by thread: Re: Stupid question about List(Of T)
- Index(es):
Relevant Pages
|