为什么要在 VBA 或 Excel 中使用 DIM 语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24378383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why should I use the DIM statement in VBA or Excel?
提问by phd443322
So there is a question on whatDIM
is, but I can't find why I want to use it.
因此,有一个上问题什么DIM
是的,但我无法找到我为什么要使用它。
As far as I can tell, I see no difference between these three sets of code:
据我所知,我认为这三组代码没有区别:
'Example 1
myVal = 2
'Example 2
DIM myVal as Integer
myVal = 2
'Example 3
DIM myVal = 2
If I omit DIM
the code still runs, and after 2 or 3 nested loops I see no difference in the output when they are omitted. Having come from Python, I like to keep my code clean*.
如果我省略DIM
代码仍然运行,并且在 2 或 3 个嵌套循环之后,当它们被省略时,我看不到输出有什么不同。来自 Python,我喜欢保持我的代码干净*。
So why should I need to declare variables with DIM
?Apart from stylistic concerns, is there a technical reason to use DIM
?
那么为什么我需要用 声明变量DIM
?除了文体问题,是否有技术原因使用DIM
?
* also I'm lazy and out of the habit of declaring variables.
* 我也很懒惰,没有声明变量的习惯。
回答by Matt Coubrough
Using Dim
makes the intentions of your code explicit and prevents common mistakes like a typo actually declaring a new variable. If you use Option Explicit On
with your code (which I thoroughly recommend) Dim
becomes mandatory.
使用Dim
使您的代码意图明确并防止常见错误,例如实际声明新变量的拼写错误。如果你使用Option Explicit On
你的代码(我完全推荐)Dim
成为强制性的。
Here's an example of failing to use Dim
causing a (potentially bad) problem:
这是一个未能使用Dim
导致(可能是坏的)问题的示例:
myVar = 100
' later on...
myVal = 10 'accidentally declare new variable instead of assign to myVar
Debug.Print myVar 'prints 100 when you were expecting 10
Whereas thiscode will save you from that mistake:
而此代码将使您免于该错误:
Option Explicit
Dim myVar as Integer
myVar = 100
' later on...
myVal = 10 ' error: Option Explicit means you *must* use Dim
More about Dim and Option Explicit here: http://msdn.microsoft.com/en-us/library/y9341s4f.aspx
有关 Dim 和 Option Explicit 的更多信息:http: //msdn.microsoft.com/en-us/library/y9341s4f.aspx
回答by chris neilsen
Any variable used without declaration is of type Variant
. While variants can be useful in some circumstances, they should be avoided when not required, because they:
任何未声明而使用的变量都是 类型Variant
。虽然变体在某些情况下很有用,但在不需要时应避免使用,因为它们:
- Are slower
- Use more memory
- Are more error prone, either through miss spelling or through assigning a value of the wrong data type
- 比较慢
- 使用更多内存
- 更容易出错,无论是拼写错误还是分配了错误数据类型的值
回答by phd443322
Moderators, I'm making an effort, assuming you'll treat me with due respect in thefuture.
版主,我正在努力,假设您将来会尊重我。
All local variables are stored on the stack as with all languages (and most parameters to functions). When a sub exits the stack is returned to how it was before the sub executed. So all memory is freed. Strings and objects are stored elsewhere in a object manager or string manager and the stack contains a pointer but vb looks after freeing it. Seting a vbstring (a bstr) to zero length frees all but two bytes. That's why we try to avoid global variables.
与所有语言(以及函数的大多数参数)一样,所有局部变量都存储在堆栈中。当 sub 退出时,堆栈将返回到 sub 执行之前的状态。所以所有的内存都被释放了。字符串和对象存储在对象管理器或字符串管理器中的其他地方,堆栈包含一个指针,但 vb 负责释放它。将 vbstring (a bstr) 设置为零长度可释放除两个字节之外的所有字节。这就是我们尽量避免使用全局变量的原因。
In scripting type programs, typeless programming has many advantages. Programs are short and use few variables so memory and speed don't matter - it will be fast enough. As programs get more complex it does matter. VB was designed for typeless programming as well as typed programming. For most excel macros, typeless programming is fine and is more readable. Vbscript only supports typeless programming (and you can paste it into vba/vb6).
在脚本类型程序中,无类型编程有很多优点。程序很短,使用的变量很少,所以内存和速度并不重要——它会足够快。随着程序变得越来越复杂,它确实很重要。VB 是为无类型编程和有类型编程而设计的。对于大多数 excel 宏,无类型编程很好并且更具可读性。vbscript 只支持无类型编程(可以粘贴到 vba/vb6 中)。