|
it
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Avoidinig globals
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 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 %> Show quote "Tim" <a*@gahnstrom.se> wrote in message news:MC2Xd.19099$d5.146369@newsb.telia.net... > 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 wrote:
> Either don't use globals at all (as in following example) or use a naming Both solutions are workable but none are really good I think.> convention for global variables like "g_var" so that you only use them > intentionally in subroutines. But if there is not globals explicit kind of directive I guess they are as good as they will be. Thanks Tim "Tim" <a*@gahnstrom.se> wrote in message Welcome to the World of Naming Conventions.news:MC2Xd.19099$d5.146369@newsb.telia.net... > 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. 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 wrote:
Show quote > "Tim" <a*@gahnstrom.se> wrote in message It sure helps but it is not fool proof, atleast not when I am the fool > news:MC2Xd.19099$d5.146369@newsb.telia.net... > >>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. 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 |
|||||||||||||||||||||||