Avoidinig globals

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Tim (al_at_gahnstrom.se)
Date: 03/07/05


Date: Mon, 07 Mar 2005 20:14:36 GMT

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



Relevant Pages

  • object lifetime
    ... dim o as object ... end sub ... goes out of scope, or i still need explicit "set o=nothing"? ...
    (microsoft.public.vb.general.discussion)
  • Re: Learning VB6 idioms
    ... >> explicit setting of objects to nothing. ... Public mConn As ADODB.Connection ... Private Sub Form_Load ... You lost your pointer to the connection created with lConn when you set lConn = mConn. ...
    (microsoft.public.vb.general.discussion)
  • Re: Cant use global FILEHANDLEs?
    ... > It's related open sub is: ... Instead, trace the filehandle toits opening make sure it is declared within an appropraite scope, and then ... The error message reffered to a lack of definition, not to improper declaration, ... That goes with the territory when using globals. ...
    (perl.beginners)
  • RE: scope of public variables
    ... "Jim Thomlinson" wrote: ... If your globals are declared anywhere other than a standard code module ... is where did you declare your variable. ... Sub optCkforDupes_Click ...
    (microsoft.public.excel.programming)
  • Re: Avoidinig globals
    ... Either don't use globals at all or use a naming ... Sub Main ... response.write'Now it prints 5 because I forgott to Dim var ...
    (microsoft.public.inetserver.asp.general)