VB2010 的隐式续行(Implicit Line Continuation)

VB2010 的隐式续行(Implicit Line Continuation)

许多情况下,您可以让 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

2、左括号之后,或右括号之前:
   
Dim  username  =  GetUsername(
    Security.Principal.WindowsIdentity.GetCurrent().Name,
    
CChar ( " \ " ),
    
1
  )

3、左大括号之后,或者右大括号之前:    

Dim  customer  =   New  Customer  With  {
  .Name 
=   " Terry Adams " ,
  .Company 
=   " Adventure Works " ,
  .Email 
=   " [email protected] "
}

4、XML 文本中的开嵌入表达式(open embedded expression)“<%=”之后,或者闭嵌入表达式(close of an embedded expression)“%>”之前:


Dim  customerXml  =   < 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' "

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)“.”之后,并且在成员名称之前。然而,当您使用 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

11、标识属性类(Attribute)时,小于号(<)之后或者大于号(>)之前。还有标识属性类时,大于号后面也可隐藏连接符。但是,当您标识程序集级别或者模块级别的属性类时,必须用连接符“_”。
    
<
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

13、For Each 语句的 In 关键字后
    
For   Each  p  In
  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 "
  }


原文参见:http://msdn.microsoft.com/en-us/library/865x40k4.aspx

你可能感兴趣的:(imp)