Option Explicit

Option Explicit

am 24.12.2007 00:54:54 von rn5a

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

Re: Option Explicit

am 24.12.2007 11:54:12 von Anthony Jones

"RN1" wrote in message
news:ed7ab016-7c15-4da4-8a4f-6d10544d454c@e10g2000prf.google groups.com...
> 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.


--
Anthony Jones - MVP ASP/ASP.NET

Re: Option Explicit

am 24.12.2007 11:57:19 von reb01501

RN1 wrote:
> 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


--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"