Solved

Dim in asp

Posted on 2002-03-26
4
1,426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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