#副標=Visual Studio 2005新功能系列(14) #大標=VB 2005 新面貌 #作者=文/沈炳宏 ==========程式========= 程式1 Sub Test() For i As Integer = 0 To 10 If i > 5 Then Continue For End If MessageBox.Show(i) Next End Sub ==========程式========= ==========程式========= 程式2 Sub Test1() Dim i As Integer = 0 Dim j As Integer = 0 While i < 10 While j < 10 i += 1 j += 1 If j > 5 Then Continue While End If MessageBox.Show("j= " & j) End While MessageBox.Show("i= " & i) End While End Sub ==========程式========= ==========程式========= 程式3 Sub Test2() Dim i As Integer = 0 Dim j As Integer = 0 Do Until i = 10 While j < 10 i += 1 j += 1 If j > 5 Then Continue Do End If MessageBox.Show("i= " & i) MessageBox.Show("j= " & j) End While Loop End Sub ==========程式========= ==========程式========= 程式4 Public Sub ChangeForm1Colors() Form1.ForeColor = System.Drawing.Color.Coral Form1.BackColor = System.Drawing.Color.Cyan Form1.Show() End Sub ==========程式========= ==========程式========= 程式5 Public Sub GetSecondInstance() Dim newForm1 As New Form1 newForm1.BackColor = System.Drawing.Color.YellowGreen newForm1.Show() End Sub ==========程式========= ==========程式========= 程式6 Dim o1, o2 As New Object If Not o1 Is o2 Then MsgBox ("o1和o2不屬於相同執行個體") If o1 IsNot o2 Then MsgBox ("o1和o2不屬於相同執行個體") ==========程式========= ==========程式========= 程式7 Imports System.Data.SqlClient Public Sub AccessSql(ByVal s As String) Using sqc As New System.Data. SqlClient.SqlConnection(s) MsgBox("連接字串: """ & sqc.ConnectionString & """") End Using End Sub ==========程式========= ==========程式========= 程式8 Public Sub declarelowerbounds() Dim monthtotal(0 To 11) As Double Dim cell(0 To 39, 0 To 19) As Integer MsgBox("所有元素個數:" _ & vbCrLf & "monthtotal (0 To 11) length " & CStr(monthtotal.Length) _ & vbCrLf & "cell (0 To 39, 0 To 19) length " & CStr(cell.Length)) End Sub ==========程式========= ==========程式========= 程式9 Public Class employee Private salaryValue As Double Protected Property salary() As Double Get Return salaryValue End Get Private Set(ByVal value As Double) salaryValue = value End Set End Property End Class ==========程式========= ==========程式========= 程式10 Public Class windowsMessage Private Declare Auto Function mb Lib "user32.dll" Alias "MessageBox" _ (ByVal hWnd As Integer, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal uType As UInteger) As Integer Private Const MB_OK As UInteger = 0 Private Const MB_ICONEXCLAMATION As UInteger = &H30 Private Const IDOK As UInteger = 1 Private Const IDCLOSE As UInteger = 8 Private Const c As UInteger = MB_OK Or MB_ICONEXCLAMATION Public Function messageThroughWindows() As String Dim r As Integer = mb(0, "測試成功!", _ "呼叫Windows API", c) Dim s As String = " MessageBox 回傳:" _ & CStr(r)& vbCrLf & "(IDOK = " & CStr(IDOK) _ & ", IDCLOSE = " & CStr(IDCLOSE) & ")" Return s End Function End Class ==========程式========= ==========程式========= 程式11 Dim numberOfChildren As Nullable(Of Integer) numberOfChildren = 2 numberOfChildren = Nothing If numberOfChildren.HasValue Then MsgBox( "有 " & CStr(numberOfChildren) & " 個小孩") Else MsgBox( "無法判斷有幾個小孩") End If ==========程式========= ==========程式========= 程式12 Public Structure Person Private Age As Integer Public Sub New(ByVal a As Double) Me.Age = a End Sub Public Shared Operator + (ByVal a1 As Person, ByVal a2 As Person) As Person Return New Person(a1.Age + a2.Age) End Operator Public Overloads Function ToString() As String Return Me.Age End Function End Structure ==========程式========= ==========程式========= 程式13 Public Sub consumeTemparature() Dim p1 As New Person(15) Dim p2 As New Person(30) Dim p3 As Person = p1 + p2 Dim s As String = p3.ToString() MsgBox(s) End Sub ==========程式========= ==========程式========= 程式14 Partial Public Class sampleClass Public Sub sub1() End Sub End Class Partial Public Class sampleClass Public Sub sub2() End Sub End Class ==========程式=========