vba中的隐式类型转换规则是什么? [英] What are the implicit type conversion rules in vba?
问题描述
我知道在 vba 中可以进行以下操作:
I know the following is possible in vba:
If Len(str) Then
Len(str)
将评估为 false
如果 Len(str)
是 0
, 否则为真
.
Len(str)
will evaluate to false
if Len(str)
is 0
, true
otherwise.
vba 的确切转换规则是什么?如果可能,请提供官方文档的链接.
What are the exact conversion rules for vba? Please provide a link to official documentation if possible.
推荐答案
boolean
变量只能有两种状态,True
和 False
.在所有编程语言中都是如此(除非存在可空数据类型,例如 C# 中的 bool?
,其中变量也可以具有值 null
表示它未定义).
A boolean
variable can have only two states, True
and False
. That's true in all programming languages (except if a nullable datatype exists, eg bool?
in C# where the variable can have also the value null
signaling it is not defined).
在 VBA 中,布尔值存储为 16 位整数.False
定义为 0,True
定义为 -1.这在其他语言中也类似,因为 0 的所有位都设置为 0,而 -1 的所有位都设置为 1.但这是一个实现细节,您的编程不应该关心它.您可以使用 AND
、OR
和 NOT
处理 True
和 False
和布尔代数.
In VBA, a boolean is stored as 16-bit integer. False
is defined as 0 and True
as -1. Thats similar in other languages, simply because 0 has all bits set to 0 and -1 has all bits set to 1. But this is an implementation detail and your programming should not care about it. You deal with True
and False
and Boolean algebra using AND
, OR
and NOT
.
隐式转换规则是直截了当的:数字数据类型如果为 0 则转换为布尔值 False
,在所有其他情况下(即,如果值any 位设置为 1).将布尔值转换为数字会导致 0 resp.-1.
Implicit conversion rules are straight forward: Numeric data types are converted to boolean False
if they are 0 and to True
in all other cases (that is, if the value has any bit set to 1). Converting a boolean value to a number results in 0 resp. -1.
Dim i As Integer
Dim b As Boolean
i = 3
b = i ' Implicit conversion from 3 to TRUE
i = b ' Implicit conversion from TRUE to -1
字符串首先被转换为数字,如果失败,你会得到一个运行时错误.日期在 VBA 中实现为数字(双精度),因此每个日期/时间都是 True
,除了日期+时间表示为 0(即 1899 年 12 月 30 日 00:00)
Strings are converted to numbers first, and if that fails, you get a runtime error. Dates are implemented as numbers (Double) in VBA, so every date/time is True
except the date+time that is represented as 0 (that is 30 Dec 1899 00:00)
但从我的角度(以及超过 30 年的编程经验)来看,隐式转换是邪恶的,应该避免.它们是无数错误的根源,它们导致代码更难阅读——而且根本没有理由依赖隐式对话.没有理由写If len(str) Then
.你要检查一个字符串的长度是否大于0,所以写成:If len(str) >0 然后
.总是.
But from my point of view (and >30 years of programming), implicit conversions are evil and should be avoided. They are source of numerous errors, they lead to code that is harder to read - and there is simply no reason to rely on implicit conversations. There is no reason to write If len(str) Then
. You want to check if the length of a string is greater than 0, so write it: If len(str) > 0 Then
. Always.
看看下面的例子:显然,VBA运行时需要执行一个隐式转换,但你能说出结果吗?
Have a look to the following example: Obviously, the VBA runtime need to execute an implicit conversion, but can you tell the outcome?
Dim i As Integer
Dim b As Boolean
i = 3
b = i
If b = i Then Debug.Print "B=I"
您可以在此处找到 Microsoft 的官方文档:https://docs.microsoft.com/en-US/office/vba/language/reference/user-interface-help/boolean-data-type
这篇关于vba中的隐式类型转换规则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!