Solved

Dim in asp

Posted on 2002-03-26
4
1,421 Views
Last Modified: 2008-03-17
Hi expert,

I just cannot see why we need to declare a variable:
Dim i
for i = ....
Can you explain?
0
Comment
Question by:wellwet
4 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 40 total points
ID: 6898142
It is ALMOST always much better, from a program design point of view, if you declare ALL of the variables that are to be used in your program.  For that reason, it is also VERY strongly suggested, though NOT required, that you also add the line,

Option Explicit

BEFORE the very first line of VBScript in EVERY ASP page that you have - this will FORCE you to declare All variables (using the Dim statement).

If you do not declare your variables, and do not use "Option Exlicit", then every varaible is created (automatically) the first time it is used.  Now you say, "YEAH, That's so easy. I want it to do that for me". But that means that if you happen to MIS_SPELL a variable name (and I am sure that just like me, you NEVER make ANY Speeling erors - Yeah right--LOL).  And if you do mis-spell a variable name, without Option Explicit and without each variable being declared, each mis-spelled variable name is TREATED as a completely NEW variable, and then you are left wondering why your program doesn't seem to wotk correctly.

If you must take short-cuts, then remember that you did it when you get lost.

Arthur Wood
0
 
LVL 18

Expert Comment

by:bruno
ID: 6898307
wellwet,

please read this article for a good explanation of WHY option explicit should always be used...


http://www.4guysfromrolla.com/webtech/faq/Intermediate/faq6.shtml



BRUNO
0
 
LVL 2

Expert Comment

by:manihopever
ID: 6898417
DIM this is keyword admired by most of the people that what is the usage and why we need to use.  yes defenitly we need to use.

Suppose you are including a file which has a variable and assign some values to it namely Procedurs.asp.  In this case unless you declare(DIM) that variable in main.asp, you cannot have the scope of that variable in main.asp.  This is the main reason why we need to use DIM.  see the example..,

procedures.asp
----------------
strUser = "wellwet"

main.asp
-----------
<% DIM strUser 'to get scope of variable at Procedures.asp%>
<!--#include File="Procedures.asp"-->

<% Response.write "User = "& strUser %>


Note:
1. Its a must to declare the variable before including the file.
2. If you declare strUser both in .asp files you will get error.

The main thing is DIM deals with the SCOPE of a varaible,object, etc.,
You can also do the same by declaring an object and get the scope in another file using "include".

Having this code you can feel how it DIM works.

thanks,
mani.v

 
0
 

Author Comment

by:wellwet
ID: 6899439
Thank you for all experts who helped me in this question.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now