Which is better way to create objects in a loop?

  • Thread starter Thread starter BobRoyAce
  • Start date Start date
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
 
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.
 
Back
Top