VB2010 的隐式续行(Implicit Line Continuation)
生活随笔
收集整理的這篇文章主要介紹了
VB2010 的隐式续行(Implicit Line Continuation)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
VB2010 的隱式續(xù)行(Implicit Line Continuation)
許多情況下,您可以讓 VB 后一行繼續(xù)前一行的語句,而不必使用下劃線(_)。下面列舉出隱式續(xù)行語法的使用情形。
1、逗號“,”之后
Public?Function?GetUsername(ByVal?username?As?String,
????????????????????????????ByVal?delimiter?As?Char,
????????????????????????????ByVal?position?As?Integer)?As?String
????Return?username.Split(delimiter)(position)
End?Function
2、左括號之后,或右括號之前:
???
Dim?username?=?GetUsername(
????Security.Principal.WindowsIdentity.GetCurrent().Name,
????CChar("\"),
????1
??)
3、左大括號之后,或者右大括號之前:?? ?
Dim?customer?=?New?Customer?With?{
??.Name?=?"Terry?Adams",
??.Company?=?"Adventure?Works",
??.Email?=?"terry@www.adventure-works.com"
}
4、XML 文本中的開嵌入表達式(open embedded expression)“<%=”之后,或者閉嵌入表達式(close of an embedded expression)“%>”之前:
??????????????????????<Name>
??????????????????????????<%=
??????????????????????????????customer.Name
??????????????????????????%>
??????????????????????</Name>
??????????????????????<Email>
??????????????????????????<%=
??????????????????????????????customer.Email
??????????????????????????%>
??????????????????????</Email>
??????????????????</Customer>
5、字符串連接符“&”之后
?? ?
cmd.CommandText?=
????"SELECT?*?FROM?Titles?JOIN?Publishers?"?&
????"ON?Publishers.PubId?=?Titles.PubID?"?&
????"WHERE?Publishers.State?=?'CA'"
6、賦值符號之后,如(=, &=, :=, +=, -=, *=, /=, \=, ^=, <<=, >>=)
?? ?
Dim?fileStream?=
??My.Computer.FileSystem.
????OpenTextFileReader(filePath)
7、表達式中二元運算符之后,如(+, -, /, *, Mod, <>, <, >, <=, >=, ^, >>, <<, And, AndAlso, Or, OrElse, Like, Xor)
?? ?
Dim?memoryInUse?=
??My.Computer.Info.TotalPhysicalMemory?+
??My.Computer.Info.TotalVirtualMemory?-
??My.Computer.Info.AvailablePhysicalMemory?-
??My.Computer.Info.AvailableVirtualMemory
8、Is 或 IsNot 運算符后
?? ?
If?TypeOf?inStream?Is
??IO.FileStream?AndAlso
??inStream?IsNot
??Nothing?Then
????ReadFile(inStream)
End?If
9、成員修飾符(member qualifier character)“.”之后,并且在成員名稱之前。然而,當(dāng)您使用 With 語句或者給類型的初始化列表(initialization list)提供成員時,必須在成員修飾符“.”后面加上下劃線“_”。當(dāng)您使用 With 語句或?qū)ο蟪跏蓟斜?#xff08;object initialization lists)時,可以在賦值符號(如“=”)后面換行。
?? ?
Dim?fileStream?=
??My.Computer.FileSystem.
????OpenTextFileReader(filePath)
...
'?不允許這樣:
'?Dim?aType?=?New?With?{?.
'????PropertyName?=?"Value"
'?可以這樣:
Dim?aType?=?New?With?{.PropertyName?=
????"Value"}
Dim?log?As?New?EventLog()
'?不可以這樣:
'?With?log
'????.
'??????Source?=?"Application"
'?End?With
'?可以這樣:
With?log
????.Source?=
??????"Application"
End?With
10、XML 軸屬性修飾符(XML axis property qualifier)后面,如“.”、“.@”、“...”的后面。然而,當(dāng)你使用 With 關(guān)鍵字時,標(biāo)識成員修飾符,你必須包含下劃線。
?? ?
Dim?customerName?=?customerXml.
??<Name>.Value
Dim?customerEmail?=?customerXml...
??<Email>.Value
11、標(biāo)識屬性類(Attribute)時,小于號(<)之后或者大于號(>)之前。還有標(biāo)識屬性類時,大于號后面也可隱藏連接符。但是,當(dāng)您標(biāo)識程序集級別或者模塊級別的屬性類時,必須用連接符“_”。
?? ?
<
Serializable()
>
Public?Class?Customer
????Public?Property?Name?As?String
????Public?Property?Company?As?String
????Public?Property?Email?As?String
End?Class
12、查詢運算符(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。若查詢運算符由多個單詞構(gòu)成,您不可以在它們中間換行,如Order By, Group Join, Take While, 和 Skip While。
????????????????????Process.GetProcesses
??????????????????Where?proc.MainWindowTitle.Contains("Visual?Studio")
??????????????????Select?proc.ProcessName,?proc.Id,
?????????????????????????proc.MainWindowTitle
13、For Each 語句的 In 關(guān)鍵字后
?? ?
For?Each?p?In
??vsProcesses
????Console.WriteLine("{0}"?&?vbTab?&?"{1}"?&?vbTab?&?"{2}",
??????p.ProcessName,
??????p.Id,
??????p.MainWindowTitle)
Next???
14、集合初始化器的 From 關(guān)鍵字后
Dim?days?=?New?List(Of?String)?From
??{
???"Mo",?"Tu",?"We",?"Th",?"F",?"Sa",?"Su"
??}
許多情況下,您可以讓 VB 后一行繼續(xù)前一行的語句,而不必使用下劃線(_)。下面列舉出隱式續(xù)行語法的使用情形。
1、逗號“,”之后
Public?Function?GetUsername(ByVal?username?As?String,
????????????????????????????ByVal?delimiter?As?Char,
????????????????????????????ByVal?position?As?Integer)?As?String
????Return?username.Split(delimiter)(position)
End?Function
2、左括號之后,或右括號之前:
???
Dim?username?=?GetUsername(
????Security.Principal.WindowsIdentity.GetCurrent().Name,
????CChar("\"),
????1
??)
3、左大括號之后,或者右大括號之前:?? ?
Dim?customer?=?New?Customer?With?{
??.Name?=?"Terry?Adams",
??.Company?=?"Adventure?Works",
??.Email?=?"terry@www.adventure-works.com"
}
4、XML 文本中的開嵌入表達式(open embedded expression)“<%=”之后,或者閉嵌入表達式(close of an embedded expression)“%>”之前:
??????????????????????<Name>
??????????????????????????<%=
??????????????????????????????customer.Name
??????????????????????????%>
??????????????????????</Name>
??????????????????????<Email>
??????????????????????????<%=
??????????????????????????????customer.Email
??????????????????????????%>
??????????????????????</Email>
??????????????????</Customer>
5、字符串連接符“&”之后
?? ?
cmd.CommandText?=
????"SELECT?*?FROM?Titles?JOIN?Publishers?"?&
????"ON?Publishers.PubId?=?Titles.PubID?"?&
????"WHERE?Publishers.State?=?'CA'"
6、賦值符號之后,如(=, &=, :=, +=, -=, *=, /=, \=, ^=, <<=, >>=)
?? ?
Dim?fileStream?=
??My.Computer.FileSystem.
????OpenTextFileReader(filePath)
7、表達式中二元運算符之后,如(+, -, /, *, Mod, <>, <, >, <=, >=, ^, >>, <<, And, AndAlso, Or, OrElse, Like, Xor)
?? ?
Dim?memoryInUse?=
??My.Computer.Info.TotalPhysicalMemory?+
??My.Computer.Info.TotalVirtualMemory?-
??My.Computer.Info.AvailablePhysicalMemory?-
??My.Computer.Info.AvailableVirtualMemory
8、Is 或 IsNot 運算符后
?? ?
If?TypeOf?inStream?Is
??IO.FileStream?AndAlso
??inStream?IsNot
??Nothing?Then
????ReadFile(inStream)
End?If
9、成員修飾符(member qualifier character)“.”之后,并且在成員名稱之前。然而,當(dāng)您使用 With 語句或者給類型的初始化列表(initialization list)提供成員時,必須在成員修飾符“.”后面加上下劃線“_”。當(dāng)您使用 With 語句或?qū)ο蟪跏蓟斜?#xff08;object initialization lists)時,可以在賦值符號(如“=”)后面換行。
?? ?
Dim?fileStream?=
??My.Computer.FileSystem.
????OpenTextFileReader(filePath)
...
'?不允許這樣:
'?Dim?aType?=?New?With?{?.
'????PropertyName?=?"Value"
'?可以這樣:
Dim?aType?=?New?With?{.PropertyName?=
????"Value"}
Dim?log?As?New?EventLog()
'?不可以這樣:
'?With?log
'????.
'??????Source?=?"Application"
'?End?With
'?可以這樣:
With?log
????.Source?=
??????"Application"
End?With
10、XML 軸屬性修飾符(XML axis property qualifier)后面,如“.”、“.@”、“...”的后面。然而,當(dāng)你使用 With 關(guān)鍵字時,標(biāo)識成員修飾符,你必須包含下劃線。
?? ?
Dim?customerName?=?customerXml.
??<Name>.Value
Dim?customerEmail?=?customerXml...
??<Email>.Value
11、標(biāo)識屬性類(Attribute)時,小于號(<)之后或者大于號(>)之前。還有標(biāo)識屬性類時,大于號后面也可隱藏連接符。但是,當(dāng)您標(biāo)識程序集級別或者模塊級別的屬性類時,必須用連接符“_”。
?? ?
<
Serializable()
>
Public?Class?Customer
????Public?Property?Name?As?String
????Public?Property?Company?As?String
????Public?Property?Email?As?String
End?Class
12、查詢運算符(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。若查詢運算符由多個單詞構(gòu)成,您不可以在它們中間換行,如Order By, Group Join, Take While, 和 Skip While。
????????????????????Process.GetProcesses
??????????????????Where?proc.MainWindowTitle.Contains("Visual?Studio")
??????????????????Select?proc.ProcessName,?proc.Id,
?????????????????????????proc.MainWindowTitle
13、For Each 語句的 In 關(guān)鍵字后
?? ?
For?Each?p?In
??vsProcesses
????Console.WriteLine("{0}"?&?vbTab?&?"{1}"?&?vbTab?&?"{2}",
??????p.ProcessName,
??????p.Id,
??????p.MainWindowTitle)
Next???
14、集合初始化器的 From 關(guān)鍵字后
Dim?days?=?New?List(Of?String)?From
??{
???"Mo",?"Tu",?"We",?"Th",?"F",?"Sa",?"Su"
??}
轉(zhuǎn)載于:https://www.cnblogs.com/feixian49/archive/2010/05/17/1737624.html
總結(jié)
以上是生活随笔為你收集整理的VB2010 的隐式续行(Implicit Line Continuation)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 梦到羊和老虎是什么意思
- 下一篇: 梦到喜欢自己的人是什么意思