Re: Stupid question about List(Of T)

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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 message
news:%23uZ6Iq4OKHA.504@xxxxxxxxxxxxxxxxxxxxxxx

"Clive Lumb" <clumb2@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> a écrit dans le
message de news: ufKXki4OKHA.4004@xxxxxxxxxxxxxxxxxxxxxxx
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.

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


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.
.



Relevant Pages

  • Re: Stupid question about List(Of T)
    ... 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. ... Dim MyList as New List(Of MyClass) ... dim TempC as new MyClass ... Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Stupid question about List(Of T)
    ... "Brian Cryer" wrote in message ... Dim MyList as New List(Of MyClass) ... dim TempC as new MyClass ... Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Stupid question about List(Of T)
    ... Dim MyList as New List(Of MyClass) ... dim TempC as new MyClass ... Do fun stuff and set TempC.ThisProperty and TempC.ThatProperty ... The above does, in fact, work, providing you leave the iDisposable implementation methods empty, but I do take your point. ...
    (microsoft.public.dotnet.languages.vb)
  • Stupid question about List(Of T)
    ... Being of advancing age and little brain, I can't for the life of me remember ... Dim MyList as New List(Of MyClass) ... dim TempC as new MyClass ...
    (microsoft.public.dotnet.languages.vb)
  • Re: A quick is this safe singleton question
    ... static readonly myClass instance = new myClass; ... public static myClass Instance ... _someObj = new SomeClass; ... constructor. ...
    (microsoft.public.dotnet.languages.csharp)