VB.NET 中跨多行的单个语句,不带下划线字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/461714/
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
Single statement across multiple lines in VB.NET without the underscore character
提问by Shawn
Is there a way to have a statement across multiple lines without the underscore character?
有没有办法在没有下划线字符的情况下跨多行声明?
It is really annoying in multi-line strings such as SQL statements, and especially LINQqueries.
在 SQL 语句等多行字符串中真的很烦人,尤其是LINQ查询。
Beside from the ugliness and difficulty when changing a line (nothing lines up anymore), you can't use comments in the middle of the multi-line statement.
除了更改一行时的丑陋和困难(不再排队)之外,您不能在多行语句的中间使用注释。
Examples of my daily personal hell.
我的日常个人地狱的例子。
dim results = from a in articles _
where a.articleID = 4 _ ' articleID - Nope, can't do this
select a.articleName
dim createArticle as string = _
"Create table article " & _
" (articleID int " & _
" ,articleName varchar(50) " & _
" )"
回答by Booji Boy
No, you have to use the underscore, but I believe that VB.NET 10 will allow multiple lines w/o the underscore, only requiring if it can't figure out where the end should be.
不,您必须使用下划线,但我相信 VB.NET 10 将允许多行不带下划线,仅当它无法确定结束位置时才需要。
回答by Darren
For most multiline strings using an XML element with an inner CDATA block is easier to avoid having to escape anything for simple raw string data.
对于大多数使用带有内部 CDATA 块的 XML 元素的多行字符串,更容易避免为简单的原始字符串数据转义任何内容。
Dim s as string = <s><![CDATA[Line 1
line 2
line 3]]></s>.Value
Note that I've seen many people state the same format but without the wrapping "< s >" tag (just the CDATA block) but visual studio Automatic formatting seams to alter the leading whitespace of each line then. I think this is due to the object inheritance structure behind the Linq "X" objects. CDATA is not a "Container", the outer block is an XElement which inherits from XContainer.
请注意,我已经看到很多人声明了相同的格式,但没有包装“< s >”标签(只是 CDATA 块),但是 Visual Studio 自动格式化接缝来改变每行的前导空白。我认为这是由于 Linq "X" 对象背后的对象继承结构。CDATA 不是“容器”,外部块是从 XContainer 继承的 XElement。
回答by IInspectable
Starting with VB.NET 10, implicit line continuationis supported for a number of syntax elements (see Statements in Visual Basic: Continuing a Statement over Multiple Lines). The concatenation operator (&) is in the list of syntax elements, that support implicit line continuation. Interspersed comments are supported as well, so your second example could be rewritten as:
从 VB.NET 10 开始,许多语法元素都支持隐式连续行(请参阅Visual Basic 中的语句:在多行上继续执行语句)。连接运算符 (&) 位于支持隐式续行的语法元素列表中。也支持穿插注释,因此您的第二个示例可以重写为:
dim createArticle as string =
"Create table article " &
" (articleID int " & ' Comment for articleID
" ,articleName varchar(50) " & ' Comment for articleName
" )"
Implicit line continuation also works for query operators, with a few restrictions:
隐式续行也适用于查询运算符,但有一些限制:
Before and after query operators (Aggregate, Distinct, From, Group By, Group Join, Join, Let, Order By, Select, Skip, Skip While, Take, Take While, Where, In, Into, On, Ascending, and Descending). You cannot break a line between the keywords of query operators that are made up of multiple keywords (Order By, Group Join, Take While, and Skip While).
前后查询运算符(Aggregate、Distinct、From、Group By、Group Join、Join、Let、Order By、Select、Skip、Skip While、Take、Take While、Where、In、Into、On、Ascending和Descending) . 您不能在由多个关键字(Order By、组加入、占用时间和跳过时间)。
Again, interspersed comments are allowed, so your first example can be rewritten as:
同样,允许穿插注释,因此您的第一个示例可以重写为:
dim results = from a in articles
where a.articleID = 4 ' articleID - now perfectly legal
select a.articleName
回答by Nathan Koop
I will say no.
我会说不。
But the only proof that I have is personal experience and the fact that documentation on line continuation doesn't have anything else in it.
但我所拥有的唯一证据是个人经验以及在线延续文档中没有任何其他内容的事实。
http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx
http://msdn.microsoft.com/en-us/library/aa711641(VS.71).aspx
回答by ChrisA
No, the underscore is the only continuation character. Personally I prefer the occasional use of a continuation character to being forced to use it always as in C#, but apart from the comments issue (which I'd agree is sometimes annoying), getting things to line up is not an issue.
不,下划线是唯一的延续字符。就我个人而言,我更喜欢偶尔使用连续字符,而不是像在 C# 中那样总是被迫使用它,但除了注释问题(我同意有时很烦人)之外,让事情排队不是问题。
With VS2008 at any rate, just select the second and following lines, hit the tab key several times, and it moves the whole lot across.
无论如何,使用 VS2008,只需选择第二行和以下行,按几次 Tab 键,它就会移动整个批次。
If it goes a tiny bit too far, you can delete the excess space a character at a time. It's a little fiddly, but it stays put once it's saved.
如果它走得太远,您可以一次删除一个字符的多余空间。它有点繁琐,但一旦保存它就会保持原样。
On the rare cases where this isn't good enough, I sometimes use the following technique to get it all to line up:
在这还不够好的极少数情况下,我有时会使用以下技术来使其全部对齐:
dim results as String = ""
results += "from a in articles "
results += "where a.articleID = 4 " 'and now you can add comments
results += "select a.articleName"
It's not perfect, and I know those that prefer C# will be tut-tutting, but there it is. It's a style choice, but I still prefer it to endless semi-colons.
它并不完美,我知道那些喜欢 C# 的人会嗤之以鼻,但就是这样。这是一种风格选择,但与无休止的分号相比,我仍然更喜欢它。
Now I'm just waiting for someone to tell me I should have used a StringBuilder ;-)
现在我只是在等待有人告诉我我应该使用 StringBuilder ;-)
回答by Pandelon
I know that this has an accepted answer, but it is the top article I found when searching for this question, and it is very out of date now. I did a little more research, and this MSDN articlecontains a list of syntax elements that implicitly continue the statement on the next line of code for VB.Net 2010.
我知道这有一个公认的答案,但它是我在搜索这个问题时找到的顶级文章,现在已经过时了。我做了更多的研究,这篇 MSDN 文章包含一个语法元素列表,这些元素隐含地延续了 VB.Net 2010 下一行代码中的语句。
回答by Nelson
Not sure if you can do that with multi-line code, but multi-line variables can be done:
不确定你是否可以用多行代码做到这一点,但多行变量可以做到:
Here is the relevant chunk:
这是相关的块:
I figured out how to use both <![CDATA[ along with <%= for variables, which allows you to code without worry.
我想出了如何同时使用 <![CDATA[ 和 <%= 变量,这让您可以放心地编码。
You basically have to terminate the CDATA tags before the VB variable and then re-add it after so the CDATA does not capture the VB code. You need to wrap the entire code block in a tag because you will you have multiple CDATA blocks.
您基本上必须在 VB 变量之前终止 CDATA 标记,然后在之后重新添加它,这样 CDATA 就不会捕获 VB 代码。您需要将整个代码块包装在一个标签中,因为您将拥有多个 CDATA 块。
Dim script As String = <code><![CDATA[
<script type="text/javascript">
var URL = ']]><%= domain %><![CDATA[/mypage.html';
</script>]]>
</code>.value
回答by Hafthor
You can use XML literals to sort of do this:
您可以使用 XML 文字来执行此操作:
Dim s = <sql>
Create table article
(articleID int -- sql comment
,articleName varchar(50) <comment text="here's an xml comment" />
)
</sql>.Value
warning: XML text rules apply, like & for ampersand, < for <, etc.
警告:适用 XML 文本规则,例如 & 对于 < 对于 < 等
Dim alertText = "Hello World"
Dim js = <script>
$(document).ready(function() {
alert('<%= alertText %>');
});
</script>.ToString 'instead of .Value includes <script> tag
note: the above is problematic when it includes characters that need to be escaped. Better to use "<script>"+js.Value+"</script>"
注意:当上面包含需要转义的字符时,上面是有问题的。更好用"<script>"+js.Value+"</script>"