VBA 变量/数组默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7626967/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:59:04  来源:igfitidea点击:

VBA variable/array default values

arraysvbadefault-value

提问by Prashant Kumar

Consider the declarations

考虑声明

Dim x As Double
Dim avg(1 To 10) As Double

What are the default values in xand avg?

什么是默认值xavg

Through testing, I want to say that xis initialized to zero. Likewise, I want to say that all elements in avgwere initialized to zero.

通过测试,我想说的x是初始化为零。同样,我想说所有元素avg都被初始化为零。

However, can I write code that depends on this? Or are the default initialization values actually indeterminate?

但是,我可以编写依赖于此的代码吗?或者默认的初始化值实际上是不确定的?

回答by Mitch Wheat

If you specify a data type but do not specify an initializer, Visual Basic initializes the variable to the default value for its data type, which for all numeric types is 0 (zero).

如果指定数据类型但未指定初始值设定项,Visual Basic 会将变量初始化为其数据类型的默认值,对于所有数字类型,默认值都是 0(零)。

When you run a macro, all the variables are initialized to a value. A numeric variable is initialized to zero, a variable length string is initialized to a zero-length string (""), and a fixed length string is filled with the ASCII code 0. Variant variables are initialized to Empty. An Empty variable is represented by a zero in a numeric context and a zero-length string ("") in a string context.

当您运行宏时,所有变量都被初始化为一个值。数字变量初始化为零,变长字符串初始化为零长度字符串(“”),固定长度字符串用ASCII码0填充。变体变量初始化为空。Empty 变量在数字上下文中由零表示,在字符串上下文中由零长度字符串 ("") 表示。

Ref.

参考

回答by Jean-Fran?ois Corbett

You canwrite code that implicitly depends on a given data type's default value, but that doesn't mean you always should, because it isn't quite as obvious what you're doing, which may confuse the next person reading your code (and that person could be you a year from now).

可以编写隐式依赖于给定数据类型的默认值的代码,但这并不意味着您总是应该这样做,因为您在做什么并不那么明显,这可能会使下一个人阅读您的代码(和一年后那个人可能是你)。

You can't go wrong with explicitly assigning an initial value to your variables. If you want, you can do that on the same line as the declaration:

为变量显式分配初始值不会出错。如果需要,您可以在声明的同一行执行此操作:

Dim x As Double: x = 0

Of course, it's a little more involved for arrays, but usually there's a convenient place to initialize them further down in your code, where you're traversing them anyway.

当然,它涉及到数组更多一点,但通常有一个方便的地方可以在代码中进一步初始化它们,无论如何你都在那里遍历它们。

回答by aevanko

Yes, you should be able to depend on number types being initialize to 0 in VBA since that's their default value.

是的,您应该能够依赖于在 VBA 中初始化为 0 的数字类型,因为这是它们的默认值。

However, if it makes you worry, and the starting values are so important, then my reccomendation is that you just explicitly assign the value 0 to them (although you really don't need to).

但是,如果它让您担心,并且起始值非常重要,那么我的建议是您只需将值 0 显式分配给它们(尽管您确实不需要)。