Option Explicit

Discussion in 'ASP General' started by RN1, Dec 23, 2007.

  1. RN1

    RN1 Guest

    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
     
    RN1, Dec 23, 2007
    #1
    1. Advertising

  2. "RN1" <> wrote in message
    news:...
    > 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
     
    Anthony Jones, Dec 24, 2007
    #2
    1. Advertising

  3. 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"
     
    Bob Barrows [MVP], Dec 24, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric Mamet
    Replies:
    2
    Views:
    828
    William Ryan
    Oct 17, 2003
  2. J.T. Conklin
    Replies:
    1
    Views:
    448
    David Hilsee
    Aug 11, 2004
  3. Andy
    Replies:
    5
    Views:
    518
    Shezan Baig
    Jan 30, 2005
  4. Jerad Rose

    VB.NET ignoring Option Explicit setting

    Jerad Rose, Feb 27, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    573
    Juan T. Llibre
    Feb 27, 2007
  5. Replies:
    1
    Views:
    587
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page