私有与公共变量和错误处理 [英] Private versus public variables and error handling

查看:79
本文介绍了私有与公共变量和错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当遇到未处理的

错误时,每个人都知道全局变量会在mdb中重新设置,但似乎这也发生在变量

时在表单级别被定义为私有。


因此,如果全局变量被重置,那么并没有说出整个故事,然后是什么

呢?

***请注意***

我不是寻找解决方案 - 我正在寻找更详细的描述

当发生未处理的错误时会发生什么 - 可能有帮助文件或

网站参考。

示例:

表单上的三个按钮,按1,2,3(导致未处理的错误)

然后返回2.重新设置m_strCode。


私有m_strCode为字符串


私有子cmdOne_Click()

m_strCode =" A1"

End Sub


Private Sub cmdTwo_Click()

MsgBox m_strCode

结束子


私人子cmdThree_Click()

MsgBox CStr(1/0)

结束子

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

推荐答案

" Anthony England" < AE ****** @ oops.co.uk>写在

news:dl ********** @nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
"Anthony England" <ae******@oops.co.uk> wrote in
news:dl**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
每个人都知道当遇到未处理的错误时,全局变量会在mdb中重新设置,但是当变量在表单级别定义为私有时,似乎也会发生这种情况。

如果全局变量被重置,那么没有说出整个故事,
然后是什么?
***请注意***
我不是在寻找解决方案 - 我正在寻找一个更详细的
描述发生未处理错误时会发生什么 -
可能有帮助文件或网站参考。

示例:
表单上的三个按钮,按下它们1,2,3(导致
未处理错误)然后返回2.重新设置m_strCode。

私有m_strCode为字符串

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

私有子cmdTwo_Click()
MsgBox m_strCode
End Sub
Private Sub cmdThree_Click()
MsgBox CStr(1/0)
End Sub
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




更改私有模块级变量函数或

自定义属性:


私有函数strCode()字符串

静态strCodeStatic字符串


如果Len(strCodeStatic)= 0那么

strCodeStatic =" A1"

结束如果

strCode = strCodeStatic

结束功能


OR:


私有属性获取strCode()为字符串

静态strCodeStatic为字符串


如果Len(strCodeStatic)= 0那么

strCodeStatic =" A1"

结束如果

strCode = strCodeStatic

结束物业


我不确定其中一个是否有任何优势。


这里的关键是将值设置为函数/属性的内部静态变量
,如果代码已重置,则透明地重新分配

静态变量。 br />

我给出的关于避免全局变量的建议也适用于

模块级变量。


-

David W. Fenton http:// www。 bway.net / ~dfenton

dfenton at bway dot net http ://www.bway.net/~dfassoc


" David W. Fenton" < DX ******** @ bway.net.invalid>在留言中写道

新闻:Xn ********************************** @ 216.196 。 97.142 ...
"David W. Fenton" <dX********@bway.net.invalid> wrote in message
news:Xn**********************************@216.196. 97.142...
" Anthony England" < AE ****** @ oops.co.uk>在
新闻中写道:dl ********** @ nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
"Anthony England" <ae******@oops.co.uk> wrote in
news:dl**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
每个人都知道当遇到
未处理的错误时,全局变量会在mdb中重新设置,但是当变量在表单级别被定义为私有时,似乎也会发生这种情况。

因此,如果全局变量被重置,没有说出整个故事,
然后是什么?
***请注意***
我不是在寻找解决方案 - 我正在寻找一个更详细的
描述发生未处理错误时会发生什么 -
可能有帮助文件或网站参考。

示例:
表单上的三个按钮,按下它们1,2,3(导致
未处理错误)然后返回2.重新设置m_strCode。

私有m_strCode为字符串

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

私有子cmdTwo_Click()
MsgBox m_strCode
End Sub
Private Sub cmdThree_Click()
MsgBox CStr(1/0)
End Sub
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



将私有模块级变量更改为函数或者
自定义属性:

私有函数strCode()As String
静态strCodeStatic为字符串

如果Len(strCodeStatic)= 0那么
strCodeStatic =& A1"
结束如果
strCode = strCodeStatic
结束函数

OR:

私有属性获取strCode()As String
静态strCodeStatic为字符串

如果Len(strCodeStatic)= 0那么
strCodeStatic =" A1"
结束如果
strCode = strCodeStatic
结束财产

我不确定其中一个是否有任何优势。

这里的关键是将值设置为静态变量你的函数/属性内部,如果代码已被重置,
静态变量将被透明地重新分配。

我已经给出了关于避免全局的建议变量也适用于
模块级变量。

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




谢谢你,大卫。正如我所提到的,这并不是一个真正的解决方法,我要求b $ b,因为我确实有一个策略来避免这种情况。这更像是一个问题

试图简明地说明发生未处理错误时会发生什么。

以前,我会说......全球变量(在外部模块中定义为public

)变量重新设置。现在我可以将其修改为...

在模块中定义为公共的变量加上在表单级别定义的变量

重新设置
$ b但是,这并不是那么简洁,也可能不是故事的结局。可以

还有其他任何东西都没有引起我的注意吗?有没有人想要

尝试清楚简洁地完成以下句子:

当发生未处理的错误时....



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....


>
因此,如果全局变量被重置,并没有说出整个故事,那么
是什么?


整层:

未处理的错误会重置变量(故事结束)。 (就是这样,

再也没有了)。


如果变量是表单的本地变量,对模块是全局变量,则无关紧要

私人或其他什么。用简单的英语,未处理的错误重新设置变量。

就是这样......完成后,所有变量都会重新设置。没有思考的东西。在这里,

但事情只是重新设定。

***请注意***
我不是在寻找解决方案 -
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 -




实际上,有一个简单的捕获所有解决方案。只需将你的用户分发一个mde到

。现在所有错误,即使你没有捕获它们也不会导致重新设置

变量。这是许多中的一个。你想要使用

mde的原因。我在本文中解释了其他一些内容:

http://www.members.shaw.ca/AlbertKal...plit/index.htm

-

Albert D. Kallal(访问MVP)

加拿大艾伯塔省埃德蒙顿
pl ***************** @ msn.com
http://www.members.shaw.ca/AlbertKallal



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


这篇关于私有与公共变量和错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆