Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Avoidinig globals

Reply
Thread Tools

Avoidinig globals

 
 
Tim
Guest
Posts: n/a
 
      03-07-2005
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
 
Reply With Quote
 
 
 
 
Mark Schupp
Guest
Posts: n/a
 
      03-07-2005
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" <(E-Mail Removed)> wrote in message
news:MC2Xd.19099$(E-Mail Removed)...
> 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



 
Reply With Quote
 
 
 
 
Tim
Guest
Posts: n/a
 
      03-08-2005
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
 
Reply With Quote
 
Phill. W
Guest
Posts: n/a
 
      03-08-2005
"Tim" <(E-Mail Removed)> wrote in message
news:MC2Xd.19099$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      03-08-2005
Phill. W wrote:
> "Tim" <(E-Mail Removed)> wrote in message
> news:MC2Xd.19099$(E-Mail Removed)...
>
>>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
different sessions and globals =?Utf-8?B?YmJkb2J1ZGR5?= ASP .Net 2 08-15-2005 05:09 PM
globals in unmanaged space marek ASP .Net 6 12-19-2003 05:23 PM
"Shared" members as globals? Ross ASP .Net 5 12-03-2003 11:34 PM
Globals Matt C++ 11 11-20-2003 01:04 AM
Re: circular dependencies & globals - avoid w/ precompiler directives?betterdesign suggestions? Suzanne Vogel C++ 2 06-26-2003 12:25 AM



Advertisments