?
Solved

Dim in asp

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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