473,735 Members | 3,971 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are static variables useful for replacing global variables?

About a year ago there was a thread about the use of global variables
in A97:

http://groups.google.com/group/comp....dc837a5aeb6157
>From the PDC 2005 presentation TLNL04 - Tips & Tricks: C++ Optimization
Best Practices by Kang Su Gatlin, casual mention was made about using
static variables as an alternative to using global variables. This
caused me to think of the following:

'-----Begin module code
Public Function MyGlobal(Option al SetValue As Variant) As Variant
Static varTemp As Variant

If Not IsMissing(SetVa lue) Then
varTemp = SetValue
MyGlobal = SetValue
Exit Function
Else
MyGlobal = varTemp
End If
End Function
'-----End module code

Supplying a value sets it and not supplying a value retrieves the last
set value. When no value was set, the function call returned an empty
string.

'-----Begin code behind form
Option Compare Database
Option Explicit

Dim varTemp As Variant

Private Sub cmdTest_Click()
Dim strSQL As String

strSQL = Nz(MyGlobal(), "It's Null.")
MsgBox (strSQL)
varTemp = MyGlobal(Null)
strSQL = Nz(varTemp, "It's Null.")
MsgBox (strSQL)
varTemp = MyGlobal("SELEC T * FROM MyTable;")
strSQL = Nz(varTemp, "It's Null.")
MsgBox (strSQL)
End Sub

Private Sub Form_Load()
varTemp = MyGlobal("SELEC T * FROM MyTable;")
End Sub
'-----End code behind form

It seems to work the way I expect. It also retained its value after I
forced an unhandled error. I also tried:

varTemp = MyGlobal(Array( "1", "2", "3"))
strSQL = Nz(varTemp(1), "It's Null.")
MsgBox(strSQL) =2

Some of the ideas from the thread were an interface, a class module and
a custom collection. In Access, is there any potential benefit in
using static variables to hold global values?

James A. Fortune
CD********@Fort uneJames.com

Nov 14 '06 #1
9 8654
>
It seems to work the way I expect. It also retained its value after I
forced an unhandled error. I also tried:
No, it will not retain the value.

So, all that trouble to define a function (that is ALSO GLOBAL) does not
gain you anything at all.
>
Some of the ideas from the thread were an interface, a class module and
a custom collection. In Access, is there any potential benefit in
using static variables to hold global values?
No, there is no benefit. If you need some global variables, then define some
global variables. End of story. However, you BETTER not be define some
variables global that DO NOT NEED TO BE GLOBAL.

So, we taking about a apples and oranges types problems. Further, your
static examples force you to define a function, and additional code (and,
worse, those functions + additional code is ALSO GLOBAL!!! - how then can
this code be merged *safely* into other exiting projects? Global functions
are just as bad as global vars.

Also, as mentioned, an un-handled error will re-set those values anyway (I
don't know how you tested this..but, they do re-set).

Anyway, the re-setting of variables is a MOOT point, and is another
different
fruit again. (since I *always* distribute a mde to my users, and un-handled
errors in a mde DO NOT re-set the variables - so, that never a problem we
need to solve is it? ).

However, the problem discussed, and the solution suggested (by that c++
developer) is not a way to use global vars, but A WAY TO AVOID using them.
We all know the evils of global vars, and the cost/difficulty in
maintaining code as such.

The problem raised here is that when you have a subroutine/function and
EXIT, then all of the values go out of scope. So, a often used (and poor)
workaround is to start declaring global variables to hold and maintain those
values. It is VERY VERY VERY important to note that in this example we are
using global vars to keep a set of variables for a PARTICULAR PIECE OF CODE.
When we say particular, we simply are NOT talking about a variable that
SHOULD BE global (but, only being done because that is what the langue
offers, or the developer is lazy). So, the rule still remains that you ONLY
should use global vars when you need them to be global.

However, the problem is that often the ONLY mechanism a developer has to
keep variables alive is global vars. So, we NEVER want to use global vars to
simply pass values to another subroutine (assuming we have other reasonable
choices). However, if scope comes into play, then a fall back is to use
global. Of course, as programming languages got better, then we received
features like "static". Static means that we can *keep* values in a routine,
and NOT have to declare global vars due to scope issues. So, the suggestion
being made is that if you don't actually need global, then don't use them.
And, often, that fallback position to use global can be eliminated by using
static variables. If that value is ONLY needed for that particular routine,
then we have NO business declaring that variables as a global..

By the way, for the sake of this discussions, global vars, global functions,
global subs, global class objects, or whatever ARE ALL THE SAME CONCEPT.
They
ALL SUFFER FROM the same problems. There is little, if ANY value in removing
a global variable, but then turning around and crating a global function to
accomplish the same thing (in fact, it means you accomplish nothing).

So,

* the issue of un-handled errors, and variables re-setting is a
separate issue from the variable "scope"

* global variables, or global functions are still evil, and
should be avoided

* The suggested idea to use static declaring for variables makes
a lot of sense

* only things that are global to the application need be
declared global

* really, no difference between global variables, and
global functions - they suffer the same ills.

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com

Nov 14 '06 #2
Albert D. Kallal wrote:

It seems to work the way I expect. It also retained its value after I
forced an unhandled error. I also tried:

No, it will not retain the value.

So, all that trouble to define a function (that is ALSO GLOBAL) does not
gain you anything at all.
Based on your comments I did some testing of a more rigorous nature and
discovered that the static values do reset as you suggest. That sinks
the idea. I take it you didn't like my idea in the other thread of
using a local table to hold global values either? I value your
comments, but please count to 10 before answering.

James A. Fortune
CD********@Fort uneJames.com

Nov 14 '06 #3
CD********@Fort uneJames.com wrote:
Albert D. Kallal wrote:
>
It seems to work the way I expect. It also retained its value after I
forced an unhandled error. I also tried:
No, it will not retain the value.

So, all that trouble to define a function (that is ALSO GLOBAL) does not
gain you anything at all.

Based on your comments I did some testing of a more rigorous nature and
discovered that the static values do reset as you suggest. That sinks
the idea. I take it you didn't like my idea in the other thread of
using a local table to hold global values either? I value your
comments, but please count to 10 before answering.

James A. Fortune
CD********@Fort uneJames.com
For many years I have used user set properties of the database or more
recently properties of access objects to hold a string representation
of any value that meets the needs one might associate with a global
variable. The coding for creating, getting and letting these properties
can be generalized to three public functions, reusable from application
to application. Their values are not reset on error. Their values live
on when the application is closed (do not have to be let or set again).
They are unique to the copy of the application being used, and by
extension to a user. They can be unique to a specific access object.

Nov 14 '06 #4
CD********@Fort uneJames.com wrote in
news:11******** **************@ b28g2000cwb.goo glegroups.com:
Some of the ideas from the thread were an interface, a class
module and a custom collection. In Access, is there any potential
benefit in using static variables to hold global values?
From the coder's perspective, there is no difference between the
type of static variable solution you gave and a property let/get.

I prefer to use static variables where there's some overhead
involved for setting the initial value, such as a User() function
that returns the Windows logon name. I use a plain old function and
a static string variable. If the static variable has Len() = 0, then
I look it up. Otherwise, I return the value of the static variable.

I hardly ever have any variables or the type that would be settable
in code. My only "globals" are things that are looked up (as
described above), or things that are constants. All other values are
stored in tables, derived from forms, or stored in data structures
like a class module.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 14 '06 #5
David W. Fenton wrote:
I hardly ever have any variables or the type that would be settable
in code. My only "globals" are things that are looked up (as
described above), or things that are constants. All other values are
stored in tables, derived from forms, or stored in data structures
like a class module.
Thanks for your comments. When you say "derived from forms" do you
mean form instances as in Terry Kreft's interface solution, values on a
hidden form, properties of the form object as in Lyle's solution, or
something else?

James A. Fortune
CD********@Fort uneJames.com

Nov 14 '06 #6
I take it you didn't like my idea in the other thread of
using a local table to hold global values either?
Not a bad idea at all.
I value your
comments, but please count to 10 before answering.
Hum, I was hard to the point, but I MOST certainly apologize if I came
across un-binding, condescending, or even arrogant.

My comments in my post are just that...my point of view. I should be allowed
to speak freely, but if I came across harsh, or in any way nasty, then do
accept my sincere apologies.

As always, ones mileage will vary on these issues, and I am *always* open to
debate here.....

I can really only speak out how my "creed" and view of development ideals
goes. My views are ceratnly not a the end all, or be all, but just the way I
see things...

--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
Nov 15 '06 #7
CD********@Fort uneJames.com wrote in
news:11******** *************@m 7g2000cwm.googl egroups.com:
David W. Fenton wrote:
>I hardly ever have any variables or the type that would be
settable in code. My only "globals" are things that are looked up
(as described above), or things that are constants. All other
values are stored in tables, derived from forms, or stored in
data structures like a class module.

Thanks for your comments. When you say "derived from forms" do
you mean form instances as in Terry Kreft's interface solution,
values on a hidden form, properties of the form object as in
Lyle's solution, or something else?
I don't use a hidden form as a data storage structure, no.

I mean that if you need to collect values in a form, you collect
them in a form. What you do with them then may depend on the
context, but for the most part, I'd think those things that are
appropriate for collecting on a form should be persisted in a table
if they should be persisted, or used immediately and then discarded.

The point is that there doesn't seem to me to be much point for
storing data in variables. Most people seem to me to use them as a
substitute for communicating directly with the source of the
information. That is, they use a form to set globals, and then use
them somewhere else. I don't think that transient data (i.e., data
that's not a constant or not persisted in a table) belongs in
variables at all, unless those are variables private to the context
in which they are used.

Basically, all I'm saying is that using globals to communicate
between Access objects is just a bad idea.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 15 '06 #8
"Albert D. Kallal" <Pl************ *******@msn.com wrote in
news:H4v6h.3237 54$R63.105628@p d7urf1no:
Hum, I was hard to the point, but I MOST certainly apologize if I
came across un-binding, condescending, or even arrogant.
You were none of those things. I don't get the sensitivity.

Of course, I'm not exactly a shrinking violet, myself...

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
Nov 15 '06 #9
David W. Fenton wrote:
I don't use a hidden form as a data storage structure, no.

I mean that if you need to collect values in a form, you collect
them in a form. What you do with them then may depend on the
context, but for the most part, I'd think those things that are
appropriate for collecting on a form should be persisted in a table
if they should be persisted, or used immediately and then discarded.

The point is that there doesn't seem to me to be much point for
storing data in variables. Most people seem to me to use them as a
substitute for communicating directly with the source of the
information. That is, they use a form to set globals, and then use
them somewhere else. I don't think that transient data (i.e., data
that's not a constant or not persisted in a table) belongs in
variables at all, unless those are variables private to the context
in which they are used.

Basically, all I'm saying is that using globals to communicate
between Access objects is just a bad idea.

--
David W. Fenton http://www.dfenton.com/
usenet at dfenton dot com http://www.dfenton.com/DFA/
I see. By "derived from forms" you mean the normal way we use forms.
I agree that using globals to communicate between Access objects is a
bad idea. I have tried to minimize their use in the past. I think
I'll plan to phase them out entirely and avoid them completely in new
code. I'll probably use a table, object properties or a class module.
Maybe I should try all the proposed techniques before deciding on which
ones I like.

James A. Fortune
CD********@Fort uneJames.com

Nov 15 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2377
by: Tony Johansson | last post by:
Hello! I know it's bad design to use global variables. I just want to ask a question about them. Is global variables and global static variables the same. These are define outside any function at the top of the a file where you have them. //Tony
17
5627
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
3
22808
by: Rahul Gandhi | last post by:
Hi, Which one preferable with respect to code size of the executable Un-initialised global variables or initialised global variables regards Rahul
7
3143
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
13
3077
by: Sunil | last post by:
Hi all, I want to know how good or bad it is using global variables i.e advantages and disadvantages of using global variables. Sunil.
25
66082
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several layers/objects/etc... or does it expect me to create a singleton global class structure? surely it's not that terrible.
44
3638
by: fabio | last post by:
Why? i' ve heard about this, the usage of global vars instead of locals is discouraged, but why? thx :)
37
2744
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
1
29371
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
8786
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9463
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9327
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9251
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4562
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2740
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.