Solved

Dim in asp

Posted on 2002-03-26
4
1,420 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
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/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

12 Experts available now in Live!

Get 1:1 Help Now