代码行末尾的单个
_(下划线)字符告诉编译器该行在下一行中继续。这允许单个语句(代码行)分散在输入文件中的多行中,这可以是一个很好的格式化帮助。
'' This Dim statement is spread across multiple lines, using the '_' character
Dim myvariable _
As Integer
这通常用于使很长的代码行更容易阅读,例如具有很多参数的过程声明:
'' Here's an example:
Declare Sub drawRectangle( ByVal x As Integer, ByVal y As Integer, ByVal w As Integer, ByVal h As Integer )
'' which can also be written as:
Declare Sub drawRectangle( ByVal x As Integer, ByVal y As Integer, _
ByVal w As Integer, ByVal h As Integer )
'' or:
Declare Sub drawRectangle _
( _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal w As Integer, _
ByVal h As Integer _
)
'' (or any other formatting you like)
_行连续字符可以插入一行代码中的任何一点。它不会在评论内。
在标识符或关键字后面添加
_行延续字符时要小心。它应该与至少一个空格字符分开,否则将被视为标识符或关键字的一部分。
'' Declare variable "a_"
'' (no line continuation happening, because the '_' character is part of
'' the "a_" identifier)
Dim As Integer a_
'' Declare variable "a" and initialize to value 5
'' (line continuation happening, because the '_' character
'' was separated from the identifier "a" with a space character)
Dim As Integer a _
= 5
警告:当使用_行连续字符将错误的代码行分散在多行数据块上时,错误消息仅指块的最后一行。