[AccessD] Global Variable

Tom Bolton tom.bolton at donnslaw.co.uk
Tue May 17 10:55:12 CDT 2005


Yip, well said! 

I see both sides of the argument, especially as an ASP developer by trade -
we all know the industry opinion on using app-wides declared in the
global.asa but we all also know we use them! I feel as though I'm committing
some terrible evil every time I'm forced into a corner and have to publicly
declare something, either in ASP or VB.
 
Eventually there has to be a payoff between the sort of code in the example
below and RAD - surely a developer who can use globals and still write
efficient, robust code is still a good developer?  It is possible...

Tom



-----Original Message-----
From: DWUTKA at marlow.com [mailto:DWUTKA at marlow.com] 
Sent: 17-May-2005 16:32
To: accessd at databaseadvisors.com
Subject: RE: [AccessD] Global Variable

Tighter?  If I looked at code like this:

Dim MyModuleLevelString as String
Private Function SetModuleLevelString(strValueToSet As String)
MyModuleLevelString=strValueToSet
End Function

I would think the developer was getting paid by the key stroke.....not that
it was 'tight code'.

Drew

-----Original Message-----
From: John W. Colby [mailto:jwcolby at colbyconsulting.com]
Sent: Tuesday, May 17, 2005 6:59 AM
To: 'Access Developers discussion and problem solving'
Subject: RE: [AccessD] Global Variable


Gustav,

This is nothing new, and not something I take credit for figuring out.  It
is not uncommon when you work with classes to have methods set the values of
and return the values from class properties.  In fact with classes you have
syntax constructs specifically for this - property get/set/let.  You can, if
you wish, dim a variable public in the class header and then it is just read
/ write to anyone with a pointer to the class.  Or you use a property get
and or set/let in whatever combination you need.  A read-only value has only
a property get.  A Write-only has only a property set (not very common) and
a read / write has a property get and property let/set.

The objective is simply to control how the variable is read/written.  Since
the variable is dimensioned private to the module (or class), only a
function in the module or class can set the value, and only a function in
the module or class can return the value.  To make a property "read only",
create only a function that returns the value, but do not build a function
that sets the value.  Then only the class or module itself can set the
variable.  

For example as the module initializes, perhaps some value is pulled from a
table and written into the variable.  Then a function returns the value to
any process that needs the value.  Or perhaps the value is calculated by
some math transformation using other values in other variables.  The other
variables are set by processes, but in order to read out the value, a
function either returns the variable directly or returns the value of the
mathematical transformation using the values in the other variables.

This is not new stuff, I did not invent it, but it is commonly used to
"replace" global variables such that the CONCEPT of a global variable exists
(can be seen from anywhere) but still protects the variable from being
written when it shouldn't be or by processes that should not be able to
write to the variable.

And yes, it is more work.  It is just one of those things that you do or
your don't.  If you accept the practice, then you start thinking about
protecting variables, using variable scope, and using functions to set /
return values where appropriate.  It becomes habit and your code becomes a
little bit tighter.

John W. Colby
www.ColbyConsulting.com 

Contribute your unused CPU cycles to a good cause:
http://folding.stanford.edu/

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Tuesday, May 17, 2005 6:30 AM
To: accessd at databaseadvisors.com
Subject: RE: [AccessD] Global Variable


Hi John

Well, here we have full control on where functions put their nasty fingers.

I haven't managed yet to build self generating code; to some this would
indicate a brave new world, to some it would be a nightmare - functions
creeping around and attacking our globals and eating our children... gosh.
But worse, they would soon learn how to take advantage of your functions to
change those "private variables"! Be prepared.

/gustav

>>> jwcolby at colbyconsulting.com 05/16 10:58 pm >>>

..  Making this a global allows functions
that have no business setting these variables to change them.  By having a
private variable returned by a public function, any function that needs the
information can get it but cannot modify it.

-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com
-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com
-------------- next part --------------

The contents of this message and any attachments are the property of Donns Solicitors 
and are intended for the confidential use of the named recipient only.  They may be legally
 privileged and should not be communicated to, or relied upon, by any other party without 
our written consent.  If you are not the addressee, please notify us immediately so that we 
can make arrangements for its return.  You should not show this e-mail to any person or
 take copies as you may be committing a criminal or civil offence for which you may be
 liable.  The statement and opinions expressed in this e-mail message are those of the 
writer, and do not necessarily represent that of Donns Solicitors.  Although any files attached
 to this e-mail will have been checked with virus protection software prior to transmission, 
you should carry out your own virus check before opening any attachment.  
Donns Solicitors does not accept any liability for any damage or loss which may be caused 
by software viruses...


More information about the AccessD mailing list