VB.Net With... End With迴圈

2019-10-16 23:02:12

這不完全是一個迴圈的結構。它執行一系列重複參照單個物件或結構的語句。

這個迴圈結構的語法是:

With object
    [ statements ]
End With

範例

Module loops
   Public Class Book
      Public Property Name As String
      Public Property Author As String
      Public Property Subject As String
   End Class
   Sub Main()
      Dim aBook As New Book
      With aBook
          .Name = "VB.Net Programming"
          .Author = "Small Su"
          .Subject = "Information Technology"
      End With
      With aBook
          Console.WriteLine(.Name)
          Console.WriteLine(.Author)
          Console.WriteLine(.Subject)
      End With
      Console.ReadLine()
   End Sub
End Module

執行上面範例程式碼,得到以下結果 -

VB.Net Programming
Small Su
Information Technology