我为只排除字符串作为输入的 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
函数必须采用的参数数量是我最大的担心。这个还不错,但是我将来可能需要制作一些其他函数,这些函数需要更多的参数,所以我主要是在寻找更好的方法来构建大参数函数。