許多情況下,您可以讓 VB 后一行繼續前一行的語句,而不必使用下劃線(_)。下面列舉出隱式續行語法的使用情形。
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
????????????????????????????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
??)
????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"
}
??.Name?=?"Terry?Adams",
??.Company?=?"Adventure?Works",
??.Email?=?"terry@www.adventure-works.com"
}
4、XML 文本中的開嵌入表達式(open embedded expression)“<%=”之后,或者閉嵌入表達式(close of an embedded expression)“%>”之前:
Dim?customerXml?=?<Customer>
??????????????????????<Name>
??????????????????????????<%=
??????????????????????????????customer.Name
??????????????????????????%>
??????????????????????</Name>
??????????????????????<Email>
??????????????????????????<%=
??????????????????????????????customer.Email
??????????????????????????%>
??????????????????????</Email>
??????????????????</Customer>
??????????????????????<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'"
????"SELECT?*?FROM?Titles?JOIN?Publishers?"?&
????"ON?Publishers.PubId?=?Titles.PubID?"?&
????"WHERE?Publishers.State?=?'CA'"
6、賦值符號之后,如(=, &=, :=, +=, -=, *=, /=, \=, ^=, <<=, >>=)
?? ?
Dim?fileStream?=
??My.Computer.FileSystem.
????OpenTextFileReader(filePath)
??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
??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
??IO.FileStream?AndAlso
??inStream?IsNot
??Nothing?Then
????ReadFile(inStream)
End?If
9、成員修飾符(member qualifier character)“.”之后,并且在成員名稱之前。然而,當您使用 With 語句或者給類型的初始化列表(initialization list)提供成員時,必須在成員修飾符“.”后面加上下劃線“_”。當您使用 With 語句或對象初始化列表(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)后面,如“.”、“.@”、“...”的后面。然而,當你使用 With 關鍵字時,標識成員修飾符,你必須包含下劃線。
?? ?
Dim?customerName?=?customerXml.
??<Name>.Value
Dim?customerEmail?=?customerXml...
??<Email>.Value
??<Name>.Value
Dim?customerEmail?=?customerXml...
??<Email>.Value
11、標識屬性類(Attribute)時,小于號(<)之后或者大于號(>)之前。還有標識屬性類時,大于號后面也可隱藏連接符。但是,當您標識程序集級別或者模塊級別的屬性類時,必須用連接符“_”。
?? ?
<
Serializable()
>
Public?Class?Customer
????Public?Property?Name?As?String
????Public?Property?Company?As?String
????Public?Property?Email?As?String
End?Class
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。若查詢運算符由多個單詞構成,您不可以在它們中間換行,如Order By, Group Join, Take While, 和 Skip While。
Dim?vsProcesses?=?From?proc?In
????????????????????Process.GetProcesses
??????????????????Where?proc.MainWindowTitle.Contains("Visual?Studio")
??????????????????Select?proc.ProcessName,?proc.Id,
?????????????????????????proc.MainWindowTitle
????????????????????Process.GetProcesses
??????????????????Where?proc.MainWindowTitle.Contains("Visual?Studio")
??????????????????Select?proc.ProcessName,?proc.Id,
?????????????????????????proc.MainWindowTitle
13、For Each 語句的 In 關鍵字后
?? ?
For?Each?p?In
??vsProcesses
????Console.WriteLine("{0}"?&?vbTab?&?"{1}"?&?vbTab?&?"{2}",
??????p.ProcessName,
??????p.Id,
??????p.MainWindowTitle)
Next
??vsProcesses
????Console.WriteLine("{0}"?&?vbTab?&?"{1}"?&?vbTab?&?"{2}",
??????p.ProcessName,
??????p.Id,
??????p.MainWindowTitle)
Next
14、集合初始化器的 From 關鍵字后
Dim?days?=?New?List(Of?String)?From
??{
???"Mo",?"Tu",?"We",?"Th",?"F",?"Sa",?"Su"
??}
??{
???"Mo",?"Tu",?"We",?"Th",?"F",?"Sa",?"Su"
??}