将多个功能组合为一个功能

时间:2018-10-14 15:22:53

标签: vb.net

我具有以下功能:

Private Sub AddToEventDetails(ByRef sEvent As My_GoogleEvent, strLabelID As String, ByRef dictLabels As Dictionary(Of String, String), strValue As String)
    Dim strFormatString = dictLabels(strLabelID)
    Dim strText = String.Format(strFormatString, strValue)

    ' strText can never be empty because it now includes the label. So test against strValue instead
    If (strValue <> "") Then
        sEvent.strEventDetails += strText & vbCrLf
    End If
End Sub

Private Sub AddToEventDetails(ByRef sEvent As My_GoogleEvent, strLabelID As String, ByRef dictLabels As Dictionary(Of String, String), strValue1 As String, strValue2 As String)
    Dim strFormatString = dictLabels(strLabelID)
    Dim strText = String.Format(strFormatString, strValue1, strValue2)

    If (strText <> "") Then
        sEvent.strEventDetails += strText & vbCrLf
    End If
End Sub

Private Sub AddToEventDetails(ByRef sEvent As My_GoogleEvent, strLabelID As String, ByRef dictLabels As Dictionary(Of String, String), strValue1 As String, strValue2 As String, strValue3 As String)
    Dim strFormatString = dictLabels(strLabelID)
    Dim strText = String.Format(strFormatString, strValue1, strValue2, strValue3)

    If (strText <> "") Then
        sEvent.strEventDetails += strText & vbCrLf
    End If
End Sub

Private Sub AddToEventDetails(ByRef sEvent As My_GoogleEvent, strLabelID As String, ByRef dictLabels As Dictionary(Of String, String), strValue1 As String, strValue2 As String, strValue3 As String, strValue4 As String)
    Dim strFormatString = dictLabels(strLabelID)
    Dim strText = String.Format(strFormatString, strValue1, strValue2, strValue3, strValue4)

    If (strText <> "") Then
        sEvent.strEventDetails += strText & vbCrLf
    End If
End Sub

Private Sub AddToEventDetails(ByRef sEvent As My_GoogleEvent, strLabelID As String, ByRef dictLabels As Dictionary(Of String, String), strValue1 As String, strValue2 As String, strValue3 As String, strValue4 As String, strValue5 As String)
    Dim strFormatString = dictLabels(strLabelID)
    Dim strText = String.Format(strFormatString, strValue1, strValue2, strValue3, strValue4, strValue5)

    If (strText <> "") Then
        sEvent.strEventDetails += strText & vbCrLf
    End If
End Sub

它们非常简单,可以正常工作。我只是想知道是否有一种不知道这些功能可以在哪里合并的简单方法?

您可以看到主要区别在于:

  1. String个参数的数量。
  2. 传递给String.Format函数的变量。

现在,我意识到我可以设置String参数optional并将其默认设置为"",然后可以使用if\else梯形图或switch。但是对于这种事情还有其他方法吗?

谢谢。

0 个答案:

没有答案