Avoidinig globals

Discussion in 'ASP General' started by Tim, Mar 7, 2005.

  1. Tim

    Tim Guest

    Is there a way to force a sub to only use global variables when
    explicitly told to?

    I mean that if I forget to dim a a local variable but there is already
    such a variable in the global scope then my function will use the global
    variable without me intending it too.

    Here is an example:

    <html>
    <% Option Explicit
    sub corect()
    Dim var
    var=5
    end sub

    sub wrong()
    var=5
    end sub

    Dim var
    var=2
    response.write(var) 'prints rightly 2
    call corect()
    response.write(var) 'Still prints rightly 2
    call wrong()
    response.write(var) 'Now it prints 5 because I forgott to Dim var
    %>
    <html>

    I would have liked to be able to declare a Globals Explicit this should
    force me to write


    sub wrong()
    global var=5
    end sub

    If I wanted wrong to change the behavior of the globaly defined variable
    var.

    Is there a way to acomplish this in ASP?

    Thanks

    Tim
     
    Tim, Mar 7, 2005
    #1
    1. Advertising

  2. Tim

    Mark Schupp Guest

    Either don't use globals at all (as in following example) or use a naming
    convention for global variables like "g_var" so that you only use them
    intentionally in subroutines.

    <%@ Language=VBScript %>
    <%
    option explicit

    call main

    Sub Main()
    Dim var
    var=2
    response.write(var) 'prints rightly 2
    call corect()
    response.write(var) 'Still prints rightly 2
    call wrong()
    response.write(var) 'Now it prints 5 because I forgott to Dim var
    End Sub

    sub corect()
    Dim var
    var=5
    end sub

    sub wrong()
    var=5
    end sub
    %>

    --
    --Mark Schupp
    Head of Development
    Integrity eLearning
    www.ielearning.com

    "Tim" <> wrote in message
    news:MC2Xd.19099$...
    > Is there a way to force a sub to only use global variables when explicitly
    > told to?
    >
    > I mean that if I forget to dim a a local variable but there is already
    > such a variable in the global scope then my function will use the global
    > variable without me intending it too.
    >
    > Here is an example:
    >
    > <html>
    > <% Option Explicit
    > sub corect()
    > Dim var
    > var=5
    > end sub
    >
    > sub wrong()
    > var=5
    > end sub
    >
    > Dim var
    > var=2
    > response.write(var) 'prints rightly 2
    > call corect()
    > response.write(var) 'Still prints rightly 2
    > call wrong()
    > response.write(var) 'Now it prints 5 because I forgott to Dim var
    > %>
    > <html>
    >
    > I would have liked to be able to declare a Globals Explicit this should
    > force me to write
    >
    >
    > sub wrong()
    > global var=5
    > end sub
    >
    > If I wanted wrong to change the behavior of the globaly defined variable
    > var.
    >
    > Is there a way to acomplish this in ASP?
    >
    > Thanks
    >
    > Tim
     
    Mark Schupp, Mar 7, 2005
    #2
    1. Advertising

  3. Tim

    Tim Guest

    Mark Schupp wrote:
    > Either don't use globals at all (as in following example) or use a naming
    > convention for global variables like "g_var" so that you only use them
    > intentionally in subroutines.


    Both solutions are workable but none are really good I think.

    But if there is not globals explicit kind of directive I guess they are
    as good as they will be.

    Thanks

    Tim
     
    Tim, Mar 8, 2005
    #3
  4. Tim

    Phill. W Guest

    "Tim" <> wrote in message
    news:MC2Xd.19099$...
    > I mean that if I forget to dim a a local variable but there is already
    > such a variable in the global scope then my function will use the
    > global variable without me intending it too.


    Welcome to the World of Naming Conventions.

    There are those who sneer at prefixes of /any/ kind on variable
    names, because they know (or, rather, their mega-expensive
    IDE tells them) what DataType a variable is and whether it's
    Private, Public, Global or whatever.

    Those of us in the Real World, who still have to fight the Good
    Fight [occasionally] with the likes of Notepad prefer to be able
    to read this kind of thing for ourselves.

    At the /very/ least, include a "Scope" prefix on your variable names,
    as in

    iCount ' [local] Integer variable
    smCount ' String Variable [at *M*odule (or class) level]
    lgCount ' Long variable [*G*lobal]

    That way, you /can't/ get your variables mixed up.

    HTH,
    Phill W.
     
    Phill. W, Mar 8, 2005
    #4
  5. Tim

    Tim Guest

    Phill. W wrote:
    > "Tim" <> wrote in message
    > news:MC2Xd.19099$...
    >
    >>I mean that if I forget to dim a a local variable but there is already
    >>such a variable in the global scope then my function will use the
    >>global variable without me intending it too.

    >
    >
    > Welcome to the World of Naming Conventions.


    > At the /very/ least, include a "Scope" prefix on your variable names,
    > as in
    >
    > iCount ' [local] Integer variable
    > smCount ' String Variable [at *M*odule (or class) level]
    > lgCount ' Long variable [*G*lobal]
    >
    > That way, you /can't/ get your variables mixed up.


    It sure helps but it is not fool proof, atleast not when I am the fool
    in question.

    I start the project out as a single page and use global variables
    (reasonable).

    When the project grows I realize I should move some stuf to functions
    and / or include files. Then I copy and past code and all the names come
    a long.

    Of course, then I should go through my copied code and change the
    prefixes but if I miss one variable there is a rather hard to find error
    in my code.

    A globals explicit would help prevent this kind of errors (or at least
    make them easy to spot). To me it is such an obviously good feature I
    assumed it should be in there somewhere but I realize I cannot get what
    is not there :(


    Tim
     
    Tim, Mar 8, 2005
    #5
    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. Ross

    "Shared" members as globals?

    Ross, Dec 3, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    387
    Jim Cheshire [MSFT]
    Dec 3, 2003
  2. marek

    globals in unmanaged space

    marek, Dec 18, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    573
    marek
    Dec 19, 2003
  3. =?Utf-8?B?YmJkb2J1ZGR5?=

    different sessions and globals

    =?Utf-8?B?YmJkb2J1ZGR5?=, Aug 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    335
    Kevin Spencer
    Aug 15, 2005
  4. Suzanne Vogel
    Replies:
    2
    Views:
    2,632
    Suzanne Vogel
    Jun 26, 2003
  5. Matt

    Globals

    Matt, Nov 14, 2003, in forum: C++
    Replies:
    11
    Views:
    3,288
    Phlip
    Nov 20, 2003
Loading...

Share This Page