C# 4.0 選擇性參數 (Optional Parameters)
可減少多載函式的建立,卻可達到相同的效果,加快使用者開發。Private Function ExecuteWithOdbc(ByVal SQL As String,
Optional ByVal parameters As Odbc.OdbcParameterCollection = Nothing) As Integer
使用上必須提供 預設數值
當不帶入參數的時候,自動帶入預設值
做到類似多載的效果
==========================================================
'下面會發生 [無法互相多載, 差異只在選擇性參數不同]
Public Function Execute(ByVal SQL As String,
Optional ByVal parameters As SqlClient.SqlParameterCollection = Nothing) As Integer
Return ExecuteWithSqlClient(SQL, parameters)
End Function
Public Function Execute(ByVal SQL As String,
Optional ByVal parameters As Odbc.OdbcParameterCollection = Nothing) As Integer
Return ExecuteWithOdbc(SQL, parameters)
End Function
'似乎可以透過傳遞物件,之後再轉型處理
Public Function Execute(ByVal SQL As String,
Optional ByVal parameters As Object = Nothing) As Integer
If m_bUseODBC Then
'透過ODBC連線,參數轉型OdbcParameterCollection
Return ExecuteWithOdbc(SQL, DirectCast(parameters, Odbc.OdbcParameterCollection))
Else
'透過SQL Client連線,參數轉型SqlParameterCollection
Return ExecuteWithSqlClient(SQL, DirectCast(parameters, SqlClient.SqlParameterCollection))
End If
End Function
沒有留言:
張貼留言