Related Posts Plugin for WordPress, Blogger...

2014年9月8日 星期一

VB.Net Snippet-測試及模式的程式碼片段

VB.Net中測試及模式的 Snippet感覺起來非常實用,
包含了許多好用項目,
其中模式類的程式碼片段包含- If,For Each,Try Catch,Property等常用程式碼

Visual Studio2013的程式碼片段12個項目列示如下,
1.ASP.NET MVC4
2.My Code程式碼片段
3.Office程式碼片段
4.Windows Form應用程式
5.Windows系統-記錄,處理序,登錄,服務
6.WPF
7.其他-連接,安全性,工作流程
8.基本-集合,資料類型,檔案系統,數學
9.應用程式-編譯,資源和設定
10.測試
11.程式碼模式-If,For Each,Try Catch,Property
12.資料-LINQ,XML,設計工具,ADO.NET

測試及模式的 Snippet內容如下,

10.測試
10.1插入測試類別: 插入測試類別。
<TestClass()>
    Public Class MyTestClass
    End Class

10.2插入測試方法: 插入測試方法。
<TestMethod()>
    Public Sub MyTestMethod()
End Sub


11.程式碼模式-If,For Each,Try Catch,Property
11.1列舉,泛型,介面,結構
11.1.1定義列舉類型: 定義列舉類型。
Enum MyEnum
        ValueOne
        ValueTwo
End Enum

11.1.2定義含有自訂值的列舉類型: 定義含有指定整數值的列舉類型。
Enum MyEnum
        ValueOne = 2
        ValueTwo = 3
End Enum

11.1.3定義新的泛型類型: 定義泛型類型。
Class GenericClass(Of T)

End Class

11.1.4定義介面: 定義介面。
Interface IMyInterface

End Interface

11.1.5定義結構: 定義結構。
Structure MyStructure
        Public ValueOne As Integer
        Public ValueTwo As Boolean
    End Structure

11.1.6尋找列舉值的名稱: 取得列舉值的字串名稱。
Dim name As String = _
    System.Enum.GetName(GetType(DayOfWeek), 0)

11.1.7定義實作 IDisposable 的類別: 實作 Dispose Finalize 模式。
Class ResourceClass
        Implements IDisposable

#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls

' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If

                ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
                ' TODO: set large fields to null.
            End If
            Me.disposedValue = True
End Sub

        ' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
        'Protected Overrides Sub Finalize()
        '    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
        '    Dispose(False)
        '    MyBase.Finalize()
        'End Sub

        ' This code added by Visual Basic to correctly implement the disposable pattern.
        Public Sub Dispose() Implements IDisposable.Dispose
            ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

End Class

11.2條件和迴圈
11.2.1使用 #If 僅建置選取的原始程式碼部分: 使用 #If 編譯器指示詞,控制要編譯到組件中的程式碼區塊。
#If True Then

#Else

#End If

11.2.2Do...Loop Until 陳述式: 執行迴圈直到條件為 True
Do

Loop Until True

11.2.3Do...Loop While 陳述式: 條件為 True 時執行迴圈。
Do

Loop While True

11.2.4Do Until...Loop 陳述式: 執行迴圈直到條件為 True
Do Until False

Loop

11.2.5Do While...Loop 陳述式: 條件為 True 時執行迴圈。
Do While True

Loop

11.2.6For Each...Next 陳述式: 在某個項目集合中執行迴圈。
For Each item As String In collectionObject

Next

11.2.7For...Next 陳述式: 在數字序列中執行迴圈。
For index = 1 To 10

Next

11.2.8If...Else...End If 陳述式: 插入 If...Else...End If 陳述式。
If True Then

Else

End If

11.2.9If...ElseIf...Else...End If 陳述式: 插入 If...ElseIf...Else...End If 陳述式。
If True Then

ElseIf False Then

Else

End If

11.2.10If..End If 陳述式: 插入 If..End If 陳述式。
If True Then

End If

11.2.11Select Case 陳述式: 插入 Select Case 陳述式。
Select Case variableName
    Case 1

    Case 2

    Case Else

End Select

11.2.12While...End While 陳述式: 插入 While...End While 陳述式。
While True

End While


11.3錯誤處理(例外狀況)
11.3.1定義 Exception 類別: 為新的例外狀況類別提供基礎實作。
Public Class ProblemException
        Inherits System.Exception

        Public Sub New(ByVal message As String)
            MyBase.New(message)
        End Sub
        Public Sub New(ByVal message As String, ByVal inner As Exception)
            MyBase.New(message, inner)
        End Sub
End Class

11.3.2擲回例外狀況: 擲回例外狀況。
Throw New ArgumentException("Exception Occured")

11.3.3Try...Catch...End Try 陳述式: 插入 Try...Catch...End Try 陳述式。
Try

Catch ex As ArgumentException

End Try

11.3.4Try...Catch...Finally...End Try 陳述式: Try...Catch...Finally...End Try 陳述式。
Try

Catch ex As ArgumentException

Finally

End Try

11.3.5Try...Finally...End Try 陳述式: 插入 Try...Finally...End Try 陳述式。
Try

Finally

End Try

11.3.6Using 陳述式: 插入 Using 陳述式。
Using resource As New Object

End Using

11.4屬性,程序,事件
11.4.1定義加法運算子 (+):在類別上定義加法 (+) 運算子。
Public Shared Operator +(ByVal class1 As ThisClass, ByVal class2 As ThisClass) As ThisClass

End Operator

11.4.2定義要呼叫 Windows 應用程式開發介面的函式: 定義要呼叫 Windows 應用程式開發介面的函式。
Declare Function Win32MessageBox Lib "user32.dll" Alias "MessageBox" (ByVal hWnd As Integer, ByVal txt As String, ByVal caption As String, ByVal type As Integer) As Integer

11.4.3定義比較運算子 > <): 定義比較運算子 (> <)
Public Shared Operator >(ByVal left As ThisClass, _
        ByVal right As ThisClass) As Boolean

End Operator
Public Shared Operator <(ByVal left As ThisClass, _
        ByVal right As ThisClass) As Boolean

End Operator

11.4.4定義 CType 運算子: 在類別上定義 CType 運算子。
Public Shared Narrowing Operator CType(ByVal initialData As String) As ThisClass
        Return New ThisClass()
End Operator
Public Shared Widening Operator CType(ByVal initialData As ThisClass) As String
        Return "Replace Me"
End Operator

11.4.5宣告事件: 宣告事件。
Public Event PropertyChanged(ByVal sender As Object, ByVal e As EventArgs)

11.4.6定義預設屬性: 定義預設屬性或索引屬性。
Default Property PropertyName(ByVal index As Integer) As String
        Get
            If True Then

            Else
                Throw New ArgumentOutOfRangeException
            End If
        End Get
        Set(ByVal Value As String)
            If True Then

            Else
                Throw New ArgumentOutOfRangeException
            End If
        End Set
End Property

11.4.7定義 Private Sub: 使用 Sub 關鍵字定義不會傳回值的私用方法。
Private Sub MyMethod()

End Sub

11.4.8定義屬性: 定義含有支援欄位的屬性。
Private newPropertyValue As String
Public Property NewProperty() As String
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As String)
            newPropertyValue = value
        End Set
End Property

11.4.9定義 Public Sub: 使用 Sub 關鍵字定義不會傳回值的方法。
Public Sub MyMethod()

End Sub

11.4.10定義等號比較運算子 (= < >): 定義等號比較運算子 (= < >)
Public Shared Operator <>(ByVal left As Example, ByVal right As Example) As Boolean

End Operator
Public Shared Operator =(ByVal left As Example, ByVal right As Example) As Boolean

End Operator

11.4.11定義含有泛型參數的函式: 定義使用泛型參數的函式。

Function MyFunction(Of T)() As Integer
        Return 0
End Function

11.4.12定義含有參數陣列的函式: 定義接受多個參數的函式。

Function MyMethod(ByVal ParamArray paramOne() As Integer) As Integer
        For index = 0 To paramOne.Length - 1

        Next
        Return 0
End Function

11.4.13定義 Overridable Sub: 定義 Overridable Sub
Overridable Sub MyMethod()

End Sub

11.4.14定義函式: 使用 Function 關鍵字定義傳回值的程序。
Function MyFunc() As Integer

        Return 0
End Function

11.4.15定義 Sub: 使用 Sub 關鍵字定義不會傳回值的程序。
Sub MySub()

End Sub

11.4.16定義 ReadOnly 屬性: 定義含有支援欄位的 ReadOnly 屬性。
Private newPropertyValue As Integer
Public ReadOnly Property NewProperty() As Integer
        Get
            Return newPropertyValue
        End Get
End Property

11.4.17定義覆寫 Dispose Sub: 依據 Dispose Finalize 模式覆寫 Dispose 函式。
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        MyBase.Dispose(disposing)
        If disposing Then
            ' TODO: dispose managed state (managed objects).
        End If
        ' TODO: free unmanaged resources (unmanaged objects).
End Sub

11.4.18定義含有 ByRef 參數的 Sub: 定義含有引數的 Sub,其引數值可由方法中的程式碼變更。
Sub MyMethod(ByRef paramName As String)

End Sub

11.4.19定義 WriteOnly 屬性: 定義含有支援欄位的 WriteOnly 屬性。
Private newPropertyValue As Integer
Public WriteOnly Property NewProperty() As Integer
        Set(ByVal value As Integer)
            newPropertyValue = value
        End Set
End Property


沒有留言:

張貼留言

頁次