为什么这是不适当的过载?

发布于 2024-11-30 05:56:57 字数 1623 浏览 0 评论 0原文

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

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

Error: cannot overload each other because they differ only by optional parameters.

One method has 3 parameters and the other has 4 parameters. What am I missing?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2024-12-07 05:56:57

可选参数使编译器困惑于应该使用哪个函数。

此外,编译器无法区分两个函数之间的 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

The optional parameters are confusing the compiler on which function it should use.

Also, the compiler can't differentiate the newValue and newName parameters between the two functions because they are both strings in the second slot.

You aren't using newName in your second function-- does that belong there?

You might want to consider something like this:

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
提笔落墨 2024-12-07 05:56:57

问题在于识别;

如果您有两个仅因一个参数是可选而签名不同的方法,则编译器无法知道您是要调用不带参数的方法 A 还是使用参数的默认值调用方法 B。

您需要重命名其中一个方法,或者将“ByValueOrDefault”一词添加到带有可选参数的方法中,或者使其成为非可选。

The problem is identification;

If you have two methods that only differ in signature because one parameter is optional, then the Compiler cannot know if you meant to call method A without the parameter or method B using the default value of the parameter.

You either need to rename one of the methods perhaps add the words 'ByValueOrDefault' to the method with the optional parameter, or make it non optional.

最好是你 2024-12-07 05:56:57

由于你的参数是可选的,当你传递3个参数时它会使用哪个函数?第一个使用可选参数,还是第二个忽略它,仅使用必需的参数和可选参数的默认值?

Because of your optional parameters, which function would it use when you pass 3 parameters? The first one using the optional parameter or the second one disregarding it, using only the required ones and the optionals' default value?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文