VB代码块中选择性注释单行报错,如何实现该操作?
在VB多行字符串拼接中注释中间行的解决方法
你遇到的错误根源在于VB的多行表达式规则:字符串拼接时,行尾的&用于标记当前表达式未结束,需要延续到下一行。注释中间行后,会破坏这个延续逻辑:
- 被注释行的上一行末尾
&没有后续拼接内容,编译器触发BC30201: Expression Expected错误 - 被注释行的下一行开头直接是字符串,缺少前置拼接符号,编译器无法识别为表达式延续,触发
BC30035: Syntax error错误
以下是两种可行的解决方法:
方法1:调整拼接符号位置
将& vbCrLf &移到每行开头(第一行除外),确保注释任意中间行时,前后行的拼接逻辑依然完整:
Dim someVariable = "this is the first line of a story..." _ & vbCrLf & "this is the second line of a story..." _ & vbCrLf & '"this is the third line, option 1, of a story..." _ & vbCrLf & "this is the third line, option 2, of a story..." _ & vbCrLf & "this is the fourth line of a story..."
也可以省略显式换行符_(VB支持运算符后自动换行),写法更简洁:
Dim someVariable = "this is the first line of a story..." & vbCrLf & "this is the second line of a story..." & vbCrLf & '"this is the third line, option 1, of a story..." & vbCrLf & "this is the third line, option 2, of a story..." & vbCrLf & "this is the fourth line of a story..."
方法2:使用XML字面量(更推荐)
VB支持XML字面量,可直接编写多行字符串,用<%-- ... --%>注释任意行,完全不会破坏语法结构,可读性更高:
Dim someVariable = <text> this is the first line of a story... this is the second line of a story... <%-- this is the third line, option 1, of a story... --%> this is the third line, option 2, of a story... this is the fourth line of a story... </text>.Value
XML字面量会自动保留换行格式,无需手动拼接vbCrLf,后续切换注释行只需添加/移除<%-- --%>即可。
内容的提问来源于stack exchange,提问作者Turnip
相关产品推荐
相关产品推荐

