473,320 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Option Explicit

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
Dec 23 '07 #1
2 1951
"RN1" <rn**@rediffmail.comwrote in message
news:ed**********************************@e10g2000 prf.googlegroups.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
Dec 24 '07 #2
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"
Dec 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: biro man | last post by:
I have a dynamic database driven page that I have been working on and tweaking etc until I have got it to work pretty much the way I want it to. I also have a defined website. I wish to include...
1
by: Robert Mark Bram | last post by:
Hi All! I have the following in a vb script: option explicit Response.Expires = -1 Server.ScriptTimeout = 600 The script gets imported in the following way: <script language="VBscript"...
7
by: Mats | last post by:
Option Explicit does not work anymore.(?) If you put <%option explicit%> at the top of your pages (direktly after the language declaration) you should get an error for each undeclared variable....
12
by: Doug Hill | last post by:
Please, Microsoft, update Option Explicit Option Strict barks at late binding. We love late binding. So Option Strict flags too many things. Option Explicit is misleading. It allows Dim...
5
by: ken | last post by:
hi All What does this command dot Option Explicit
6
by: Brett | last post by:
I find there is more casting required in C# than VB.NET. If Option Strict/Explicit is turned on, will this basically create the same environment as C# - uppercase, more casting required, must...
9
by: YYZ | last post by:
After reading many messages in this group, it seems that the preferred setting for this is ON. Okay, I did that in my project (first with ..Net -- long time VB6 developer) and now a bunch of...
6
by: John Kotuby | last post by:
Hi all, I am simply trying to include the Option Explicit declaration at the top of an ASP page and am getting an error: Error Type: Microsoft VBScript compilation (0x800A0400) Expected...
0
by: Michael Jenck | last post by:
Hi All, I have been playing with the codedom and can't get it to output for option compare Binary with a CodeCompileUnit. I have searched the web and Don't now if it's possible. If it's not...
1
by: Jimmbo | last post by:
I get an option explicit error saying it does not appear as the first line whenever I use it with utf-8 charset encoding on my Sun Cobalt server running Chillisoft on Linux. The error does not...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.