PDA

View Full Version : Diming?


Morgoth
07-09-2002, 08:07 AM
I am just confused on this, I noticed I don't really need to declair any veriable i use, so is it really nessacary with ASP?

What do you think personaly, and do you dim every veriable you can?

Thanks, and Good night :)

glenngv
07-09-2002, 08:20 AM
This is from www.asp101.com

Dim Even When Not Using Option Explicit
--------------------------------------------------------------------------------
If for any reason you ever find yourself not using Option Explicit (IE: maintaining someone else's old code), you should still dimension all the variables you use. A dimensioned variable works almost twice as fast as an implicitly created variable.

If you do a speed test on:


For lngCnt = 1 To 1000000000
Next


compared to:


Dim lngCnt

For lngCnt = 1 To 1000000000
Next


you will find that the second one finishes much faster.

When you dimension your variables, it's best to do so at the top of the page. Some people tend to dimension them just before they are used, but that makes it difficult to see what variables are used, often leads to less readable code, and can even cause problems:

A Dim statement is always executed, even if it is inside a code section that isn't. This is because the Dim statements are executed first when compiling to reserve the required memory. For example, this will give you a "Name Redefined" error:


If False Then
Dim lngCnt '<---- Always Executed
End If

Dim lngCnt '<---- Will Cause an Error

Morgoth
07-09-2002, 02:27 PM
Thank you. That's alwasy nice to know...

whammy
07-10-2002, 02:06 AM
Not to mention if you forget to Dim a variable and you're NOT using Option Explicit, and you add functionality to your application later on, you might just bang your head against the wall for awhile wondering why you're not getting any results :)

( Can you tell this post is from experience? ;) )

Morgoth
07-10-2002, 04:25 AM
Answer to original post:

Dim all!

oracleguy
07-10-2002, 09:59 PM
Plus all ASP .NET stuff has option explicit on by default. So it's just good practice.

Also it can prevent errors on your part, like if you dimension something as an integer you can accidently use it to hold a string. If you dimension something but dont specify what type of variable it is then it is either:

Pre-.NET: Variant
.NET: Object

whammy
07-12-2002, 04:08 AM
I guess the moral of this story is to Dimension all variables in ASP. :)

Thread closed. :)