Option Explicit

R

RN1

Using Option Explicit necessitates that any variable used has to be
declared (Dimmed) explicitly but it DOESN'T necessitate that the
variable HAS TO BE DECLARED BEFORE the variable is used. For e.g. the
code below, which first uses the variable "str" & then declares it,
works fine:

===============================
<% Option Explicit %>
<%
str="Hello World"
Response.Write(str)
Dim str
%>
===============================

Why so? If I am not mistaken, usually it's the other way round with
other languages where variables have to be declared before they are
used. Correct me if I am wrong.

Thanks,

Ron
 
A

Anthony Jones

RN1 said:
Using Option Explicit necessitates that any variable used has to be
declared (Dimmed) explicitly but it DOESN'T necessitate that the
variable HAS TO BE DECLARED BEFORE the variable is used. For e.g. the
code below, which first uses the variable "str" & then declares it,
works fine:

===============================
<% Option Explicit %>
<%
str="Hello World"
Response.Write(str)
Dim str
%>
===============================

Why so? If I am not mistaken, usually it's the other way round with
other languages where variables have to be declared before they are
used. Correct me if I am wrong.

It becomes easier to understand when you bear in mind that Dim (and Redim)
is not executed in the same way as other lines of code. It tells the parser
that the current scope should allocate a variable and associate it with the
specified identifier. This is done as parse time not run time. The whole
script is parsed before any execution is done (before it moves into 'run
time').

Hence at the point where execution of the code reaches str="Hello World" the
str identifier has already been associated with a variable since that was
done in the earlier parse phase.

IOW, most of the time it doesn't matter where in any scope you place your
Dims (although personally I prefer to place them at the top).

The exception is where the Dim defines an array. Whilst the variable to
hold the reference to the array is created at parse time, the array itself
is created at runtime at the point in the code where the Dim statement is.
 
B

Bob Barrows [MVP]

RN1 said:
Using Option Explicit necessitates that any variable used has to be
declared (Dimmed) explicitly but it DOESN'T necessitate that the
variable HAS TO BE DECLARED BEFORE the variable is used. For e.g. the
code below, which first uses the variable "str" & then declares it,
works fine:

===============================
<% Option Explicit %>
<%
str="Hello World"
Response.Write(str)
Dim str
%>
===============================

Why so? If I am not mistaken, usually it's the other way round with
other languages where variables have to be declared before they are
used. Correct me if I am wrong.
Because the first step performed by the vbscript/jscript compiler is to pull
(hoist) all the declarations to the top of the scope. Here is some light
reading:

http://blogs.msdn.com/ericlippert/archive/2004/06/18/159378.aspx

and

http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top