我想知道如果任何人能夠幫助我重構我前一段時間寫的一些代碼。我爲一個COM對象編寫了一個包裝器,只將字符串作爲輸入,所以在良好的OOP實踐中,我將字符串包裝在函數中,以便構建和調用。幫助重構函數,需要大量的參數
我只是想知道如果有人能想到更好的方式來做下面的代碼。
Public Function OpenTable(ByVal TablePath As String, Optional ByVal OpenAs As String = Nothing, _
Optional ByVal Hide As Boolean = False, Optional ByVal AsReadOnly As Boolean = False, _
Optional ByVal Interactive As Boolean = True, Optional ByVal Password As String = Nothing, _
Optional ByVal NoIndex As Boolean = False, Optional ByVal ViewAutomatic As Boolean = True) As TableInfo
If String.IsNullOrEmpty(TablePath) Then
Throw New ArgumentNullException("TablePath", "TablePath cannot be null or empty")
End If
Dim Builder = New StringBuilder("Open Table ")
Builder.AppendFormat("{0}{1}{2}", ControlChars.Quote, TablePath, ControlChars.Quote)
If (Not String.IsNullOrEmpty(OpenAs)) Then Builder.AppendFormat(" as {0} ", OpenAs)
If (Hide) Then Builder.Append(" Hide ")
If (AsReadOnly) Then Builder.Append(" ReadOnly ")
If (Interactive) Then Builder.Append(" Interactive ")
If (Not String.IsNullOrEmpty(Password)) Then Builder.AppendFormat(" Password {0} ", Password)
If (NoIndex) Then Builder.Append(" NoIndex ")
If (ViewAutomatic) Then Builder.Append(" View Automatic ")
MyComApp.Do(Builder.ToString)
Dim FileInfo = New IO.FileInfo(TablePath)
Return New TableInfo(FileInfo.Name.Substring(0, InStrRev(FileInfo.Name, ".") - 1))
End Function
函數必須採用的參數量是我最擔心的。這個並不算太壞,但我還有其他一些功能可能在將來需要更多的參數,所以我主要尋找更好的方法來構建大型參數函數。
謝謝。
反對...爲什麼? – 2008-12-08 13:36:38