473,703 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I prevent the loss of the values assigned to global variables after an untrapped run-time error?

MLH
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
single bttn-clik, the way I've been doing it ever since Access 2.0 is
by closing and then reopening the app. That gets cumbersome.
But its the only way I've ever known. Just wondering.
Nov 13 '05 #1
17 5624

MLH wrote:
A97 Topic: If there is a way to preserve the values assigned to
global variables when an untrapped runtime error occurs? I think not.
During development, I'm constantly running tests on imperfect code.

I think so.

Nov 13 '05 #2
MLH
On 11 Oct 2005 11:25:37 -0700, "lylefair" <ly***********@ aim.com>
wrote: You should know - You've helped me fix enough of it.
During development, I'm constantly running tests on imperfect code.

I think so.


Nov 13 '05 #3
On Tue, 11 Oct 2005 14:12:26 -0400, MLH <CR**@NorthStat e.net> wrote:
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
single bttn-clik, the way I've been doing it ever since Access 2.0 is
by closing and then reopening the app. That gets cumbersome.
But its the only way I've ever known. Just wondering.


One method is, instead of global variables, at least the key ones you want to keep,
use controls on an invisible form.

Nov 13 '05 #4
MLH
Now there's an idea. Thx.

One method is, instead of global variables, at least the key ones you want to keep,
use controls on an invisible form.


Nov 13 '05 #5
MLH wrote:
single bttn-clik, the way I've been doing it ever since Access 2.0 is
by closing and then reopening the app. That gets cumbersome.


Then:

1) don't use global variables.

Or

2) Include proper error handling.

Or

3) Store values on a hidden form

My preference is to do all three.
--
Tim http://www.ucs.mun.ca/~tmarshal/
^o<
/#) "Burp-beep, burp-beep, burp-beep?" - Quaker Jake
/^^ "Whatcha doin?" - Ditto "TIM-MAY!!" - Me
Nov 13 '05 #6
MLH <CR**@NorthStat e.net> wrote in
news:ta******** *************** *********@4ax.c om:
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
single bttn-clik, the way I've been doing it ever since Access 2.0
is by closing and then reopening the app. That gets cumbersome.
But its the only way I've ever known. Just wondering.


First off, if your app uses lots of global variables, then there's
probably something wrong with it.

Global constants are a different thing -- those are fine, but they
also don't get lost on reset because they are hardwired into the
compiled p-code.

You need to re-evaluate all the cases where you are using global
variables and figure out if you can replace them with self-healing
objets.

For years, I used a global variable to store a reference to the
current database for use in place of every declaration of Set db =
CurrentDB(). But that wouldn't survive a code reset during
development, so I started doing this:

If dbGlobal Is Nothing Then Set dbGlobal = CurrentDB()
Set rs = dbGlobal.OpenRe cordset([SQL])

If you replace the global variable with a function that returns the
data type of the global variable, that function can "heal" itself:

Function dbGlobal() As DAO.Database
Static db As Database

If db Is Nothing Then Set db = CurrentDB()
dbGlobal = db
End Function

The way this works is that, with a STATIC variable, the first time
it's called, it initializes the internal STATIC variable, and as
long as there's not a code reset, just returns the value of the
STATIC variable. If there's a code reset, the STATIC variable gets
cleared and when the function is called it will be re-initialized.

This principle can be applied to any number of global variables that
are used to store application-wide data (such as form colors or any
other application-specific data).

Now, the problem is: where do you store the persistent data?

Well, you can have a table or you can use custom properties of the
MDB file. Why, then, not just look them up each time you use them?
Because it's much slower to do a table lookup up or to find the
value of a database custom property than it is to check a global
variable or get the value from a public function.

Now, as you can see, I'm limiting this to global variables that are
used for storing global values that are permanent throughout a
working session, and have only one value that is not changed after
the variable is initialized.

All global variables that change values at runtime SHOULD BE
ENTIRELY ELIMINATED FROM AN APPLICATION.

For passing data between forms, it's much better to *not* use global
variables. I tend to use class modules for this, as simple data
storage structures, because class modules can be self-initializing,
and because they provide a single intermediate structure for
mediating between runtime objects. That is, if you want form1 to get
data from form2, a class module makes it much easier to manage.
Form2 sets up the class module, and then form1 gets its data from
the class module. This makes it much easier to get the same data
from different forms, because the receiving form needs to know only
about the class module -- it doesn't need to know anything at all
about the source form.

That's the idea behind using global variables for that purpose, but
that has the downside of:

1. losing values on a code reset with no way to automatically
re-initialize them.

2. values don't clear themselves when they are obsolete, so you
could accidentally use data that was a different context.

With a class module-based structure you initialize an instance of
the class for the immediate purpose, populate its values, use the
values in a different context, then when done, destroy the instance,
thus clearing stored values.

The key issue here is that you just need to get away from using
global variables as much as possible. If you do that, you end up
with better code and less annoyance during coding.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #7
MLH
Thx David, for the thorough reply. I certainly have
a handful on my plate now. Yes, I used lots of them
and can see not that it was not a wise decision.
Nov 13 '05 #8
One of my forms has a great deal of internal state information,
most of it loaded from a table when the form opens. If I reset
during debug, the state is lost, and the form no longer works.

It is not self-healing: I am forced to close and re-open the form.

What would be your approach to this situation?

(david)

"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@24. 168.128.90...
MLH <CR**@NorthStat e.net> wrote in
news:ta******** *************** *********@4ax.c om:
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
single bttn-clik, the way I've been doing it ever since Access 2.0
is by closing and then reopening the app. That gets cumbersome.
But its the only way I've ever known. Just wondering.


First off, if your app uses lots of global variables, then there's
probably something wrong with it.

Global constants are a different thing -- those are fine, but they
also don't get lost on reset because they are hardwired into the
compiled p-code.

You need to re-evaluate all the cases where you are using global
variables and figure out if you can replace them with self-healing
objets.

For years, I used a global variable to store a reference to the
current database for use in place of every declaration of Set db =
CurrentDB(). But that wouldn't survive a code reset during
development, so I started doing this:

If dbGlobal Is Nothing Then Set dbGlobal = CurrentDB()
Set rs = dbGlobal.OpenRe cordset([SQL])

If you replace the global variable with a function that returns the
data type of the global variable, that function can "heal" itself:

Function dbGlobal() As DAO.Database
Static db As Database

If db Is Nothing Then Set db = CurrentDB()
dbGlobal = db
End Function

The way this works is that, with a STATIC variable, the first time
it's called, it initializes the internal STATIC variable, and as
long as there's not a code reset, just returns the value of the
STATIC variable. If there's a code reset, the STATIC variable gets
cleared and when the function is called it will be re-initialized.

This principle can be applied to any number of global variables that
are used to store application-wide data (such as form colors or any
other application-specific data).

Now, the problem is: where do you store the persistent data?

Well, you can have a table or you can use custom properties of the
MDB file. Why, then, not just look them up each time you use them?
Because it's much slower to do a table lookup up or to find the
value of a database custom property than it is to check a global
variable or get the value from a public function.

Now, as you can see, I'm limiting this to global variables that are
used for storing global values that are permanent throughout a
working session, and have only one value that is not changed after
the variable is initialized.

All global variables that change values at runtime SHOULD BE
ENTIRELY ELIMINATED FROM AN APPLICATION.

For passing data between forms, it's much better to *not* use global
variables. I tend to use class modules for this, as simple data
storage structures, because class modules can be self-initializing,
and because they provide a single intermediate structure for
mediating between runtime objects. That is, if you want form1 to get
data from form2, a class module makes it much easier to manage.
Form2 sets up the class module, and then form1 gets its data from
the class module. This makes it much easier to get the same data
from different forms, because the receiving form needs to know only
about the class module -- it doesn't need to know anything at all
about the source form.

That's the idea behind using global variables for that purpose, but
that has the downside of:

1. losing values on a code reset with no way to automatically
re-initialize them.

2. values don't clear themselves when they are obsolete, so you
could accidentally use data that was a different context.

With a class module-based structure you initialize an instance of
the class for the immediate purpose, populate its values, use the
values in a different context, then when done, destroy the instance,
thus clearing stored values.

The key issue here is that you just need to get away from using
global variables as much as possible. If you do that, you end up
with better code and less annoyance during coding.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc

Nov 13 '05 #9
"MLH" <CR**@NorthStat e.net> wrote in message
news:ta******** *************** *********@4ax.c om...
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.


Yes, there is:

Simply distribute a mde to your users.

I hate to admit it, but a lot of my very small routines do NOT have error
handling, and I happen to think that they don't requite it.

So, if you use a mde, then un-handled errors don't re-set local, or global
vars...

So, this is just one of many reasons why a mde is the preferred choice, and
is what you should distribute to your users.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 13 '05 #10

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

Similar topics

1
3571
by: Jonathan | last post by:
Thanks for the help and commentary on sessions. I had another question or two on a related topic. I installed a newer version of php and went in to set it to read global variables, which was originally set to 'off' when I installed it. The notes in the php.ini file said that it's less secure to allow reading them. What kind of security risks would be associated with this? For example, in my site, I just want to be able to read the...
11
2700
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's called $glb. So, every function so far looks like this: function something() { global $glb; }
14
3162
by: smilemaster | last post by:
I have recently started experiencing form data loss. Here is an example: ====test_form1.php==== <form method="POST" action="test_form2.php"> <input name="textboxvalue" type="text" size="20"> <br> <input type="submit" value="Submit"> </form>
9
8109
by: DesignerNut | last post by:
Can a javascript array value be used in an onclick event? I haven't been successful in getting it to work so I'm wondering if it even can be done. onclick="calculate('some array value'); I've tried: onclick="calculate('<script type="text/javascript">document.write(points);</script>');
6
5268
by: AAVF | last post by:
Hi We have a problem with a query. An Access database links via ODBC to a UNIX server. To speed things, we use the ODBC to load the relevant tables to the local PC that runs Access so that all querying is done locally. One of the reports we run allows the user to list all invoices within a period. They are also allowed to select a customer code and a product set on
2
1870
by: MLH | last post by:
Can I use Access Basic to dump global var's & values to immediate window in Access 2.0? Can I use it in Access 97 to do the same thing? And please, I can obviously run the following... Debug.Print "MyGlobalVar = "; MyGlobalVar .... that's not what I'm looking for here. I wanna do
4
12334
by: Chris | last post by:
Hi, I think I'm having some problems here with garbage collection. Currently, I have the following code: public struct Event { public int timestamp;
29
3048
by: John Wood | last post by:
Even though CSC does its best to detect use of unassigned variables, it often misses it... for example if you just declare a double in a class without assigning a default value, it has a default value of 0 and lets you use it anyway. My question is, how can I retrieve the default value for a given type? The CLR obviously has these defaults stored somewhere and I was hoping to get hold of it. I hoped the following code would work, but...
27
2806
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a recent thread. I want to know why this descrimination is in place. Can't all the variables be initialised to NULL automatically by the compiler? This would make programming a little easier.
0
264
by: hiddenhippo | last post by:
Hi, I have a class which defines and instantiates all my global application level variables; for example, namespace Constants { public sealed class UserActionIdAbbr { public static string Abbr = {"PEN","FIN","ACC","REJ","DEC"};
0
8758
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9251
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
9121
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
9017
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,...
0
8962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5922
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();...
1
3123
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
2450
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2069
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.