所以有一个问题关于what DIM is https://stackoverflow.com/questions/1033507/what-does-dim-stand-for-in-visual-basic-and-basic,但我找不到为什么要使用它。
据我所知,我认为这三组代码之间没有区别:
'Example 1
myVal = 2
'Example 2
DIM myVal as Integer
myVal = 2
'Example 3
DIM myVal = 2
如果我省略DIM
代码仍然运行,并且在 2 或 3 个嵌套循环之后,当省略它们时,我发现输出没有任何差异。由于来自 Python,我喜欢保持代码干净*。
那么为什么我需要声明变量DIM
?除了风格方面的考虑之外,是否还有使用的技术原因DIM
?
* also I'm lazy and out of the habit of declaring variables.
Using Dim
使代码的意图明确并防止常见错误,例如实际声明新变量的拼写错误。如果你使用Option Explicit On
用你的代码(我强烈推荐)Dim
成为强制性的。
这是一个使用失败的例子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 this代码可以让你避免这个错误:
Option Explicit
Dim myVar as Integer
myVar = 100
' later on...
myVal = 10 ' error: Option Explicit means you *must* use Dim
有关 Dim 和 Option Explicit 的更多信息请参见此处:http://msdn.microsoft.com/en-us/library/y9341s4f.aspx http://msdn.microsoft.com/en-us/library/y9341s4f.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)