MSAccess:全局与局部变量声明 [英] MSAccess: Global vs Local Variable Declaration

查看:57
本文介绍了MSAccess:全局与局部变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几十个模块的 Access 应用程序.我一直在每个新模块中重新声明变量,减去需要全局作用域的变量.我到处都使用 Option Explicit.

I have an Access app with a couple dozen modules. I have been declaring variable afresh in each new module, minus the variable requiring global scope. I use Option Explicit everywhere.

问题:将我的公共局部变量声明为全局变量是否是一种糟糕的做法,即使范围只是局部的?.我想只是为了方便:

Question: Would it be poor practice to declare my common local variables as globals, even though the scope is only local?. Just a matter of convenience, I suppose:

我在每个模块中声明的变量示例:

Examples of variables I declare in each and every module:

Dim strMsg as String
Dim strSQL as String
Dim N, N1, N2 as Integer
Dim db As Database

Set db = CurrentDb

任何指导或建议?

推荐答案

我认为您仍然希望将这些类型的变量保留在本地.如果您在 Module1 中并设置 strMsg 然后从 Module1 调用 Module2 中的函数并修改 <Module2 中的 code>strMsg.返回到 Module1 后,你会期望 Module1 中的状态没有改变,但现在你的 strMsg 是不同的.当然,这些基本上是静态类,本身没有状态,但通常最好使所有内容尽可能靠近调用代码.如果您没有真正令人信服的理由需要将这些变量设为全局变量,那么请将它们保留为本地变量.

I think you would still want to keep those kinds of variables local. What if you are in Module1 and set strMsg then you call a function in Module2 from Module1 and you modify strMsg in Module2. having returned to Module1 you would expect that no state in Module1 to have changed but now your strMsg is different. Sure these are basically static classes and don't have state per-se but it's usually best to keep everything as close to the calling code as possible. if you don't have a really compelling reason to need these variables to be global then keep them local.

这篇关于MSAccess:全局与局部变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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