Which is better way to create objects in a loop?

B

BobRoyAce

Which is the better way to do things? Is there practically any
difference, or are they essentially the same?

Scenario #1
Dim oList as New List(Of MyObject)
For i As Integer = 1 To 10
Dim oObject as New MyObject
oObject.Property1 = 2
oObject.Property2 = 3
oList.Add(oObject)
Next

Scenario #2
Dim oList as New List(Of MyObject)
Dim oObject as MyObject
For i As Integer = 1 To 10
oObject = New MyObject
oObject.Property1 = 2
oObject.Property2 = 3
oList.Add(oObject)
Next
 
A

Armin Zingler

Am 28.07.2011 02:01, schrieb BobRoyAce:
Which is the better way to do things? Is there practically any
difference, or are they essentially the same?

Scenario #1
Dim oList as New List(Of MyObject)
For i As Integer = 1 To 10
Dim oObject as New MyObject
oObject.Property1 = 2
oObject.Property2 = 3
oList.Add(oObject)
Next

Scenario #2
Dim oList as New List(Of MyObject)
Dim oObject as MyObject
For i As Integer = 1 To 10
oObject = New MyObject
oObject.Property1 = 2
oObject.Property2 = 3
oList.Add(oObject)
Next

It's the same. Only difference is visibility of oObject outside the loop.
No practical difference in this snippet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top