Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dim in asp

Posted on 2002-03-26
4
Medium Priority
?
1,431 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 160 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
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/…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 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