Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Option Explicit

Reply
Thread Tools

Option Explicit

 
 
RN1
Guest
Posts: n/a
 
      12-23-2007
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
 
Reply With Quote
 
 
 
 
Anthony Jones
Guest
Posts: n/a
 
      12-24-2007
"RN1" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Bob Barrows [MVP]
Guest
Posts: n/a
 
      12-24-2007
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/ar...18/159378.aspx

and

http://blogs.msdn.com/ericlippert/ar...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"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.NET ignoring Option Explicit setting Jerad Rose ASP .Net 1 02-27-2007 02:02 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM
How can I set Option Explicit systematically in VB.Net? Eric Mamet ASP .Net 2 10-16-2003 11:56 PM



Advertisments