Skip to content

续行符


续行符允许将单行代码分散到多行中。

说明

在代码行末尾的单个 '_'(下划线)字符告诉编译器该行在下一行继续。这允许将单个语句(代码行)分散到输入文件的多行中,这可以是一种很好的格式化辅助手段:

start GeSHi

vb
'' This Dim statement is spread across multiple lines, using the '_' character
Dim myvariable _
As Integer

end GeSHi

这通常用于使非常长的代码行更易于阅读,例如带有大量参数的过程声明:

start GeSHi

vb
'' 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)

end GeSHi

'_' 续行符几乎可以在代码行的任何位置插入。但它在注释内不起作用。

在标识符或关键字后面紧接着添加 '_' 续行符时要小心。它应与至少一个空格字符分隔,否则它将被视为标识符或关键字的一部分:

start GeSHi

vb
'' 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

end GeSHi

警告:当使用 '_' 续行符将错误的代码行分散到多行块中时,错误消息只引用块的最后一行。

fbc 版本 >= 1.08 的注意事项:

在宏/define 的使用中,使用 '##' 来转义续行符 '',以允许将宏展开代码的多行合并为单个语句。

另请参阅

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系