为什么这是一个不正确的过载?

时间:2011-08-19 16:09:25

标签: .net vb.net asp.net-3.5 overloading

错误:不能相互重载,因为它们只有可选参数不同。

一种方法有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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

由于您的可选参数,当您传递3个参数时,它将使用哪个函数?第一个使用可选参数,第二个使用可选参数,只使用必需的参数和选项的默认值?

答案 2 :(得分:0)

问题在于识别;

如果你有两个方法只有签名不同,因为一个参数是可选的,那么编译器无法知道你是否打算在没有参数或方法B的情况下使用参数的默认值来调用方法A.

您需要重命名其中一个方法,或者使用可选参数将单词'ByValueOrDefault'添加到方法中,或者使其不可选。

相关问题