VBA中的'Type characters'和'Type conversion functions'有什么区别?
What is the difference between 'Type characters' and 'Type conversion functions' in VBA?
据我对VBA的理解,好像类型字符(MS Docs - Type characters)主要用在变量的声明过程中,缩短代码行并使用隐式声明,同时仍然强制数据类型。
另一方面,类型转换函数(MS Docs - Type conversion functions)主要用于计算和值转换,以确保变量之间的类型兼容性。
但我看到 类型字符 被用在公式中,而 类型转换函数 本来可以用。那么这两种方法有什么区别呢?我应该在什么时候使用哪一个?最佳做法是什么?
例如下面代码中b
的各个计算有什么区别:
Dim a As Integer, b As Double
a = 4
b = a# * 10.0
b = a * 10#
b = CDbl(a * 10)
b = CDbl(a) * CDbl(10)
如果这个问题很愚蠢或者已经在本网站的某个地方得到回答,请原谅。
请查看下一个子示例和每行注释:
Sub testTypeCharConv()
Dim a As Integer, b As Double
a = 4.2 'in order to see how Integer declaration trunks the decimals
'b = a# * 10.2 'this will raise an error, since the variable has already been declared
b = a * 1000 'it returns correct 4000 (4.2 is rounded at 4, being an Integer)
'b = a * 10000 'Overflow error, because of Integer maximum value of 32,767
b = a * 10000# 'returns 40000 due to Double conversion, the result is converted to Double
b = CDbl(a) * 1000 'it does not change anything. 4.2 has already been trunked to 4
b = CDbl("4.2") * 1000 'returns 4200, CDbl converting a string to a double
b = a * 10.2 'returns 40.8
b = CDbl(a) * CDbl(10.2) 'both conversions do not help in any way
End Sub
当声明变量或函数时,类型字符是显式而非隐式选项。它们只是 As Datatype
.
的一个更晦涩、更不受欢迎的版本
当使用变量或函数时,根本不需要类型字符。它们可以作为个人偏好保留,但是它们必须与声明变量的类型相匹配(这就是为什么首先不需要它们),并且声明是否使用 #
或 As Double
。然而,在这种情况下,代码开始看起来像 QBasic.
一个值得注意的例外是 的使用,它带有 Variant
和 String
两种风格。指定 $
会有所不同。
而且在声明文字时,为什么会有人想要将文字降级为函数调用?这样做的唯一结果是代码变慢,使未来的开发人员感到困惑,并且无法将表达式的结果保存在 Const
中,因此当您想要 10-as-a-Double
、10#
远远优于 CDbl(10)
,就像 #10/19/2012 11:34:06 AM#
is 远远优于 CDate("2012-10-19 11:34:06")
.
据我对VBA的理解,好像类型字符(MS Docs - Type characters)主要用在变量的声明过程中,缩短代码行并使用隐式声明,同时仍然强制数据类型。
另一方面,类型转换函数(MS Docs - Type conversion functions)主要用于计算和值转换,以确保变量之间的类型兼容性。
但我看到 类型字符 被用在公式中,而 类型转换函数 本来可以用。那么这两种方法有什么区别呢?我应该在什么时候使用哪一个?最佳做法是什么?
例如下面代码中b
的各个计算有什么区别:
Dim a As Integer, b As Double
a = 4
b = a# * 10.0
b = a * 10#
b = CDbl(a * 10)
b = CDbl(a) * CDbl(10)
如果这个问题很愚蠢或者已经在本网站的某个地方得到回答,请原谅。
请查看下一个子示例和每行注释:
Sub testTypeCharConv()
Dim a As Integer, b As Double
a = 4.2 'in order to see how Integer declaration trunks the decimals
'b = a# * 10.2 'this will raise an error, since the variable has already been declared
b = a * 1000 'it returns correct 4000 (4.2 is rounded at 4, being an Integer)
'b = a * 10000 'Overflow error, because of Integer maximum value of 32,767
b = a * 10000# 'returns 40000 due to Double conversion, the result is converted to Double
b = CDbl(a) * 1000 'it does not change anything. 4.2 has already been trunked to 4
b = CDbl("4.2") * 1000 'returns 4200, CDbl converting a string to a double
b = a * 10.2 'returns 40.8
b = CDbl(a) * CDbl(10.2) 'both conversions do not help in any way
End Sub
当声明变量或函数时,类型字符是显式而非隐式选项。它们只是 As Datatype
.
当使用变量或函数时,根本不需要类型字符。它们可以作为个人偏好保留,但是它们必须与声明变量的类型相匹配(这就是为什么首先不需要它们),并且声明是否使用 #
或 As Double
。然而,在这种情况下,代码开始看起来像 QBasic.
一个值得注意的例外是 Variant
和 String
两种风格。指定 $
会有所不同。
而且在声明文字时,为什么会有人想要将文字降级为函数调用?这样做的唯一结果是代码变慢,使未来的开发人员感到困惑,并且无法将表达式的结果保存在 Const
中,因此当您想要 10-as-a-Double
、10#
远远优于 CDbl(10)
,就像 #10/19/2012 11:34:06 AM#
is 远远优于 CDate("2012-10-19 11:34:06")
.