vba VBA中变量名后可能有哪些后缀?

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

What are possible suffixes after variable name in VBA?

vba

提问by archimed7592

As I've already figured out, there is at least six of them: !@#$%&.

正如我已经知道的,至少有六个:!@#$%&.

Here is snip:

这是截图:

Dim A!, B@, C#, D$, E%, F&
Debug.Print "A! - " & TypeName(A)
Debug.Print "B@ - " & TypeName(B)
Debug.Print "C# - " & TypeName(C)
Debug.Print "D$ - " & TypeName(D)
Debug.Print "E% - " & TypeName(E)
Debug.Print "F& - " & TypeName(F)

Outputs

输出

A! - Single
B@ - Currency
C# - Double
D$ - String
E% - Integer
F& - Long

Where is documentation on this syntax sugar?

关于这个语法糖的文档在哪里?

What other possible suffixes are there?

还有哪些其他可能的后缀?

Is there one for Date?

有日期吗?

采纳答案by ktharsis

A full(?) list is here http://support.microsoft.com/kb/110264under Variable and Function Name Prefixes. As Remou said - they are not recommended (the article says that use is "discouraged"). I believe you covered them all in your list. Date is technically a variant (stored as a floating point) so no shortcut for that one.

完整 (?) 列表位于http://support.microsoft.com/kb/110264变量和函数名称前缀下。正如 Remou 所说 - 不推荐使用它们(文章称“不鼓励”使用)。我相信你在你的清单中涵盖了所有这些。日期在技术上是一种变体(存储为浮点数),因此没有捷径。

回答by Mathieu Guindon

These suffixes are type hints, and the link in the accepted answer is outdated.

这些后缀是类型提示,已接受答案中的链接已过时。

Dim someInteger% '% Equivalent to "As Integer"
Dim someLong&    '& Equivalent to "As Long"
Dim someDecimal@ '@ Equivalent to "As Currency"
Dim someSingle!  '! Equivalent to "As Single"
Dim someDouble#  '# Equivalent to "As Double"
Dim someString$  '$ Equivalent to "As String"

Dim someLongLong^  '^ Equivalent to "As LongLong" in 64-bit VBA hosts

So you had them all, except ^for LongLong, introduced in VBA7 for 64-bit host applications. Exactly why Microsoft introduced a new type hint for a new value type is beyond me though.

所以,你有他们所有,除^用于LongLong在VBA7推出了64位主机应用程序。不过,微软为新值类型引入新类型提示的确切原因超出了我的理解。

It's more syntax poisonthan syntax sugarthough, and dates all the way back from ancestral, dinosaurian versions of BASIC before the Asclause was a thing, for example in this Commodore 64 BASIC 2.0 fizzbuzz code:

尽管如此,它比语法糖更具有语法毒药,并且可以追溯到在条款出现之前的 BASIC 的祖先恐龙版本,例如在这个 Commodore 64 BASIC 2.0 fizzbuzz 代码中:As

1000 REM INIT VARIABLES
1010 LET FIZZ$ = "FIZZ"
1011 LET BUZZ$ = "BUZZ"
1020 LET FIZZ% = 3
1021 LET BUZZ% = 5
1030 LET MIN% = 1
1031 LET MAX% = 15
1100 PRINT FIZZ$ + ":" + STR$(FIZZ%)
1101 PRINT BUZZ$ + ":" + STR$(BUZZ%)
1102 PRINT FIZZ$ + BUZZ$ + ":" + STR$(FIZZ%*BUZZ%)
1105 PRINT

As you can see, type hints aren't the only paleo-code that VBA supports: line numbers, Remcomments, and explicit Letvalue assigments were also a thing in 1982. Avoid them at all costs.

如您所见,类型提示并不是 VBA 支持的唯一古代码:行号、Rem注释和显式Let值分配也是 1982 年的事情。不惜一切代价避免它们。

In literals, prefer explicit conversions over type hints:

在文字中,更喜欢显式转换而不是类型提示:

Debug.Print TypeName(32&) 'prints Long
Debug.Print TypeName(CLng(32)) 'prints Long

Ask yourself not whether you can, ask yourself whether you should.-- unknown

不要问自己能不能,而要问自己是否应该-- 未知

回答by Fionnuala

These are long since deprecated and only remain for backward compatibility AFAIK. Declare your variables as the type required. You can also coerce variables to type.

这些早已被弃用,仅保留用于向后兼容 AFAIK。将变量声明为所需的类型。您还可以强制变量键入。