473,699 Members | 2,773 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Private versus public variables and error handling

Everyone knows that global variables get re-set in an mdb when an un-handled
error is encountered, but it seems that this also happens when the variable
is defined as private at form-level.

So if "global variables get re-set" doesn't tell the whole story, then what
does?
***please note***
I'm not looking for a solution - I'm looking for a more detailed description
of what happens when an un-handled error occurs - possibly with help file or
website reference.
Example:
Three buttons on a form, press them 1,2,3 (which causes an un-handled error)
then back to 2. m_strCode is re-set.

Private m_strCode As String

Private Sub cmdOne_Click()
m_strCode = "A1"
End Sub

Private Sub cmdTwo_Click()
MsgBox m_strCode
End Sub

Private Sub cmdThree_Click( )
MsgBox CStr(1 / 0)
End Sub
Nov 13 '05 #1
21 4407
"Anthony England" <ae******@oops. co.uk> wrote in
news:dl******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com:
Everyone knows that global variables get re-set in an mdb when an
un-handled error is encountered, but it seems that this also
happens when the variable is defined as private at form-level.

So if "global variables get re-set" doesn't tell the whole story,
then what does?
***please note***
I'm not looking for a solution - I'm looking for a more detailed
description of what happens when an un-handled error occurs -
possibly with help file or website reference.
Example:
Three buttons on a form, press them 1,2,3 (which causes an
un-handled error) then back to 2. m_strCode is re-set.

Private m_strCode As String

Private Sub cmdOne_Click()
m_strCode = "A1"
End Sub

Private Sub cmdTwo_Click()
MsgBox m_strCode
End Sub

Private Sub cmdThree_Click( )
MsgBox CStr(1 / 0)
End Sub


Change the private module-level variable either to a function or a
custom property:

Private Function strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Function

OR:

Private Property Get strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Property

I'm not sure that there's any advantage to one or the other.

The key here is that the value is set to a static variable internal
to your function/property, and if the code has been reset, the
static variable is re-assigned transparently.

The advice I've given about avoiding global variables applies to
module-level variables, too.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #2
"David W. Fenton" <dX********@bwa y.net.invalid> wrote in message
news:Xn******** *************** ***********@216 .196.97.142...
"Anthony England" <ae******@oops. co.uk> wrote in
news:dl******** **@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com:
Everyone knows that global variables get re-set in an mdb when an
un-handled error is encountered, but it seems that this also
happens when the variable is defined as private at form-level.

So if "global variables get re-set" doesn't tell the whole story,
then what does?
***please note***
I'm not looking for a solution - I'm looking for a more detailed
description of what happens when an un-handled error occurs -
possibly with help file or website reference.
Example:
Three buttons on a form, press them 1,2,3 (which causes an
un-handled error) then back to 2. m_strCode is re-set.

Private m_strCode As String

Private Sub cmdOne_Click()
m_strCode = "A1"
End Sub

Private Sub cmdTwo_Click()
MsgBox m_strCode
End Sub

Private Sub cmdThree_Click( )
MsgBox CStr(1 / 0)
End Sub


Change the private module-level variable either to a function or a
custom property:

Private Function strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Function

OR:

Private Property Get strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Property

I'm not sure that there's any advantage to one or the other.

The key here is that the value is set to a static variable internal
to your function/property, and if the code has been reset, the
static variable is re-assigned transparently.

The advice I've given about avoiding global variables applies to
module-level variables, too.

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


Thanks for that, David. As I mentioned, it's not really a workaround I'm
looking for as I do have a stratgegy to avoid this. It's more a question of
trying to concisely state what happens when an un-handled error occurs.
Previously, I would have said "... global variables (those defined as public
in an external module) get re-set". Now I might amend that to "...
variables defined as public in modules plus those defined at form level get
re-set"
However, that's not that concise and may not be the end of the story. Could
there be any other bits that have escaped my attention? Does anyone want to
try to complete the following sentence clearly and concisely:
When an un-handled error occurs....

Nov 13 '05 #3
>
So if "global variables get re-set" doesn't tell the whole story, then
what does?
The whole storey:
A unhandled error will reset variables (end of story). (that is it,
nothing more, nothing less).

It does not matter if the variable is local to a form, global to a module,
private or whatever. In plain English, a un-handled error re-sets variables.
That is it...done, all variables get re-set. There is no "thinking" here,
but things are simply re-set.
***please note***
I'm not looking for a solution -


Actually, there is a simply catch all solution. Just distribute a mde to
your users. All errors now, even when you don't trap them will NOT cause
variables to be re-set. This is one of "many" reasons why you want to use a
mde. I explain a few others in this article of mine:

http://www.members.shaw.ca/AlbertKal...plit/index.htm
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 14 '05 #4
> However, that's not that concise and may not be the end of the story.
Could there be any other bits that have escaped my attention? Does anyone
want to try to complete the following sentence clearly and concisely:
When an un-handled error occurs....


all variables get reset.

(that is 4 words, and is the end of the story).

You can see my other post, as I mentioned that using a mde will NOT reset
variables, and is on reason why they are far more reliable for your users.
--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
pl************* ****@msn.com
http://www.members.shaw.ca/AlbertKallal
Nov 14 '05 #5
Albert D. Kallal wrote:
Actually, there is a simply catch all solution. Just distribute a mde to
your users. All errors now, even when you don't trap them will NOT cause
variables to be re-set. This is one of "many" reasons why you want to use a
mde. I explain a few others in this article of mine:


It is not a reason why I want to use an mde. After an unhandled error I
want to require that the application be closed and, maybe, in some
situations, restarted, .. probably not. [A BSD woouldn't really upset
me]. A note to the developer would be a nice touch because the
unhandled arror shouldn't happen; it should be handled or fixed.

To those who say, "Oh, what an inconvenience!" , I say. "Learn your
trade!"; if you are just dabbling then dabble better.

Nov 14 '05 #6
> It is not a reason why I want to use an mde. After an unhandled
want to require that the application be closed and, maybe, in some
Runtime does this to .mdb's. Unhandled errors cause the application to
close.

In an MDE, on unhandled errors the Runtime version discards the custom error
message, substitutes it's own error message, and continues.

(david)
"lylefair" <ly***********@ aim.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com... Albert D. Kallal wrote:
Actually, there is a simply catch all solution. Just distribute a mde to
your users. All errors now, even when you don't trap them will NOT cause
variables to be re-set. This is one of "many" reasons why you want to use
a
mde. I explain a few others in this article of mine:


It is not a reason why I want to use an mde. After an unhandled error I
want to require that the application be closed and, maybe, in some
situations, restarted, .. probably not. [A BSD woouldn't really upset
me]. A note to the developer would be a nice touch because the
unhandled arror shouldn't happen; it should be handled or fixed.

To those who say, "Oh, what an inconvenience!" , I say. "Learn your
trade!"; if you are just dabbling then dabble better.

Nov 14 '05 #7
On Sun, 13 Nov 2005 15:56:43 -0600, "David W. Fenton"
<dX********@bwa y.net.invalid> wrote:
"Anthony England" <ae******@oops. co.uk> wrote in
news:dl******* ***@nwrdmz01.dm z.ncs.ea.ibs-infra.bt.com:
Everyone knows that global variables get re-set in an mdb when an
un-handled error is encountered, but it seems that this also
happens when the variable is defined as private at form-level.

So if "global variables get re-set" doesn't tell the whole story,
then what does?
***please note***
I'm not looking for a solution - I'm looking for a more detailed
description of what happens when an un-handled error occurs -
possibly with help file or website reference.
Example:
Three buttons on a form, press them 1,2,3 (which causes an
un-handled error) then back to 2. m_strCode is re-set.

Private m_strCode As String

Private Sub cmdOne_Click()
m_strCode = "A1"
End Sub

Private Sub cmdTwo_Click()
MsgBox m_strCode
End Sub

Private Sub cmdThree_Click( )
MsgBox CStr(1 / 0)
End Sub
Change the private module-level variable either to a function or a
custom property:

Private Function strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Function

OR:

Private Property Get strCode() As String
Static strCodeStatic As String

If Len(strCodeStat ic) = 0 Then
strCodeStatic = "A1"
End If
strCode = strCodeStatic
End Property

I'm not sure that there's any advantage to one or the other.

The key here is that the value is set to a static variable internal
to your function/property, and if the code has been reset, the
static variable is re-assigned transparently.


I use the same approach. It works very nicely.
The advice I've given about avoiding global variables applies to
module-level variables, too.


But, of course, private module-level variables are preferrable to global
because at least you can tell what procedures could touch the variables.
Also, we should add that module-level variables in class modules are very
appropriate (though not in massive numbers per module).
Nov 14 '05 #8
On Mon, 14 Nov 2005 00:53:19 GMT, "Albert D. Kallal" <ka****@msn.com > wrote:
However, that's not that concise and may not be the end of the story.
Could there be any other bits that have escaped my attention? Does anyone
want to try to complete the following sentence clearly and concisely:
When an un-handled error occurs....


all variables get reset.

(that is 4 words, and is the end of the story).

You can see my other post, as I mentioned that using a mde will NOT reset
variables, and is on reason why they are far more reliable for your users.


But of course, the same cases that reset variables in an MDB exit an MDE
entirely. Basically, avoid all unhandled errors because they'll cause
problems for your users. At least, put error handlers in all event handler
procedures unless they do nothing but call other procedures with error
handlers, and do not resolve expressions ion parameter expressions.
Nov 14 '05 #9
"Steve Jorgensen" <no****@nospam. nospam> wrote in message
news:5a******** *************** *********@4ax.c om...
On Mon, 14 Nov 2005 00:53:19 GMT, "Albert D. Kallal" <ka****@msn.com > wrote:
But of course, the same cases that reset variables in an MDB exit an MDE
entirely.


I believe you are describing what happens in the runtime, which has nothing to
do with the file being an MDE. Even in the runtime it is not entirely true that
an unhandlled error always causes the app to quit, but that is the most common
result.

--
I don't check the Email account attached
to this message. Send instead to...
RBrandt at Hunter dot com
Nov 14 '05 #10

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

Similar topics

3
3385
by: Parintas Themis STE Kardias | last post by:
Hi How can i use public variables. These variables i wand to use in different places on a form (like a button) and are neccessery to write the type of variable like: Public VAR1, var2 as integer or Public VAR1, var2 Thanks
14
3884
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the maxRequestLength parameter of the <httpRuntime> section and that works as expected. What I want is to enforce a max file size but haven't been able to trap the error thrown when the file is too large and that's where I could use some help.
3
2055
by: Mr Newbie | last post by:
I'm testing error handling configurations and having some trouble. I created a WebForm called. ErrDefault.aspx and I am trying to use the Page error attribute to force the redirection to a custom page, but I only get and unhandled exception page and it does not direct me to my specific page. I'm probably doing something really stupid, but I cant see what . Any Ideas ? - Thanx Mr N --------- DETAILS BELOW -----------
4
7611
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As Integer) As SqlDataReader ' Declare the SQL data layer class Dim oSQL As New...
3
2854
by: Stefan Johansson | last post by:
Hi all I'am moving from Visual Foxpro and have a question regarding "best practice" error handling in vb .net. In VFP I have always used a "central" error handling object in order to have a easy and reusable way of handling all errors in a program. The VB 6 coding examples I have seen there has always been error handling code in each program module.
9
2226
by: Gustaf | last post by:
I'm confused about structured error handling. The following piece of code is a simplification of a class library I'm working on. It works, and it does what I want, but I'm still not convinced that I have been doing it right. I think I overdo it. Please have a look: -- using System; using System.IO;
10
2285
by: Anthony England | last post by:
(sorry for the likely repost, but it is still not showing on my news server and after that much typing, I don't want to lose it) I am considering general error handling routines and have written a sample function to look up an ID in a table. The function returns True if it can find the ID and create a recordset based on that ID, otherwise it returns false. **I am not looking for comments on the usefulness of this function - it is
5
1734
by: csgraham74 | last post by:
Hi guys, Basically i have been developing in dotnet for a couple of years but ive had a few issues in regards to error handling. For example - I have a class that i call passing in a stored procedure and connection string as a path. My method returns a dataset. In my SP i have an output parameter which tells me whether the SP select is successful or not. If i get a error code passed back then i throw an exception this then returns...
0
11588
by: Lysander | last post by:
Thought I would give something back with a few articles. This article is a bit of code to add error handling. When I have time, I want to write articles on multilingual databases, and Access Security, but I'll start with something short and simple This code was written in Access 2003 but should be valid in Access 2000 By default, when you start a new module, either in a form or report, or a global module, Access does not declare Option...
0
8704
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
8623
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
9187
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
7777
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5879
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
4390
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
3071
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
2361
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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.