2011-08-19 48 views
1

錯誤:不能相互重載,因爲它們只有可選參數不同。爲什麼這是一個不適當的超載?

一種方法有3個參數,另一種有4個參數。我錯過了什麼?

Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode) 

    Dim resxNodesList As List(Of ResXDataNode) = getResourceData() 
    For i As Integer = 0 To resxNodesList.Count - 1 
     If resxNodesList.Item(i).Name = keyCtrl Then 
      Dim name As String = resxNodesList.Item(i).Name 
      Dim comment As String = resxNodesList.Item(i).Comment 
      Dim newResxNode As ResXDataNode = New ResXDataNode(name, newValue) 
      newResxNode.Comment = comment 

      resxNodesList.RemoveAt(i) 
      resxNodesList.Add(newResxNode) 
      Exit For 
     End If 
    Next 

    Return resxNodesList 
End Function 

Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newName As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode) 

    Dim resxNodesList As List(Of ResXDataNode) = getResourceData() 
    For i As Integer = 0 To resxNodesList.Count - 1 
     If resxNodesList.Item(i).Name = keyCtrl Then 
      Dim name As String = resxNodesList.Item(i).Name 
      Dim comment As String = resxNodesList.Item(i).Comment 
      Dim newResxNode As ResXDataNode = New ResXDataNode(name, newValue) 
      newResxNode.Comment = comment 

      resxNodesList.RemoveAt(i) 
      resxNodesList.Add(newResxNode) 
      Exit For 
     End If 
    Next 

    Return resxNodesList 
End Function 

回答

4

可選參數讓編譯器在應該使用哪個函數時感到困惑。

此外,編譯器無法區分這兩個函數之間的newValue和newName參數,因爲它們都是第二個插槽中的字符串。

你沒有在第二個函數中使用newName--它屬於那裏嗎?

你可能要考慮這樣的事情:

Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newValue As String) As List(Of ResXDataNode) 
    Return updateResxNodes(keyCtrl, newValue, String.Empty, String.Empty) 
End Function 

Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newValue As String, ByVal newName As String) As List(Of ResXDataNode) 
    Return updateResxNodes(keyCtrl, newValue, newName, String.Empty) 
End Function 

Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newValue As String, ByVal newName As String, ByVal newComment As String) As List(Of ResXDataNode) 
    Dim resxNodesList As List(Of ResXDataNode) = getResourceData() 
    For i As Integer = 0 To resxNodesList.Count - 1 
    If resxNodesList.Item(i).Name = keyCtrl Then 
     Dim name As String = resxNodesList.Item(i).Name 
     Dim comment As String = resxNodesList.Item(i).Comment 
     Dim newResxNode As ResXDataNode = New ResXDataNode(name, newValue) 
     newResxNode.Comment = comment 
     resxNodesList.RemoveAt(i) 
     resxNodesList.Add(newResxNode) 
     Exit For 
    End If 
    Next 
    Return resxNodesList 
End Function 
+0

啊。我懂了。謝謝。 – dotnetN00b

0

因爲你的可選參數,當你通過3個參數,將使用哪種功能?第一個使用可選參數或第二個忽略它,只使用必需的參數和選項的默認值?

0

問題在於識別;

如果由於一個參數是可選的,您有兩種方法僅在簽名上有所不同,那麼編譯器無法知道您是否打算使用參數的默認值調用方法A而不使用參數或方法B.

您可能需要重命名其中一個方法,可能會將具有可選參數的方法添加爲「ByValueOrDefault」字樣,或者使其不可選。