PC Review


Reply
Thread Tools Rate Thread

Declaring Vars in a loop

 
 
Chris
Guest
Posts: n/a
 
      26th Oct 2004
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


 
Reply With Quote
 
 
 
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      26th Oct 2004
"Chris" <(E-Mail Removed)> schrieb:
> 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...

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

 
Reply With Quote
 
Chris Dunaway
Guest
Posts: n/a
 
      26th Oct 2004
On Tue, 26 Oct 2004 15:02:50 -0500, Chris wrote:

> 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.
 
Reply With Quote
 
=?Utf-8?B?RGVubmlz?=
Guest
Posts: n/a
 
      26th Oct 2004
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" wrote:

> On Tue, 26 Oct 2004 15:02:50 -0500, Chris wrote:
>
> > 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.
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pivot tables - can change row/col vars but can't get rid of existing vars Andreww Microsoft Excel Programming 0 25th May 2007 11:50 AM
Declaring variables in a loop RD Microsoft Access VBA Modules 2 14th Mar 2006 06:37 PM
Declaring variables in a For Next loop wandoledzep@hotmail.com Microsoft VB .NET 7 1st Nov 2005 05:32 PM
declaring variables in a counted loop =?Utf-8?B?aXQgd29ya3MtaXQganVzdCB3b250IGRvIGFueXRo Microsoft C# .NET 1 31st Oct 2005 03:32 AM
declaring active cell inside a for loop and offsetting from it l1075 Microsoft Excel Programming 2 5th May 2004 08:40 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:36 PM.