Declaring Vars in a loop

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris
 
Chris said:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious.

Compile both versions and take a look at the IL using the "ILDASM.EXE"
tool...
 
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris

As far as Dim'ing the object, you are still creating 1000 objects. In Snip
2, the Obj variable is only visible inside the For loop. Wheras with the
first, the Object is visible after the for loop exits.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Aren't you really just creating a single reference to an object, obj.
Doesn't the loop just reassign an object reference for GetSomeTypeOfObject()
to obj. You can't have more than one obj variable can you? I was surprised
that the dim inside the loop runs.
..

Chris Dunaway" <"dunawayc[[at]_lunchmeat said:
Is there any difference (memory/speed wise) between these two snipits. I
always like to write in snipit 2 style, but was just curious. I'm scared
I'd declare a thousand pointers when I don't need to.

Snip 1:
Dim Obj as object
For ii as integer = 0 to 1000
Obj = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Snip 2:
For ii as integer = 0 to 1000
Dim Obj as object = GetSomeTypeOfObject()
DoSomeThingWithObject(Obj)
Next

Thanks for the insite.
Chris

As far as Dim'ing the object, you are still creating 1000 objects. In Snip
2, the Obj variable is only visible inside the For loop. Wheras with the
first, the Object is visible after the for loop exits.

--
Chris

dunawayc[AT]sbcglobal_lunchmeat_[DOT]net

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 

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

Back
Top