为什么这是不适当的过载?
错误:不能互相重载,因为它们仅在可选参数上有所不同。
一种方法有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可选参数使编译器困惑于应该使用哪个函数。
此外,编译器无法区分两个函数之间的 newValue 和 newName 参数,因为它们都是第二个槽中的字符串。
您没有在第二个函数中使用 newName ——它属于那里吗?
您可能需要考虑这样的事情:
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:
问题在于识别;
如果您有两个仅因一个参数是可选而签名不同的方法,则编译器无法知道您是要调用不带参数的方法 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.
由于你的参数是可选的,当你传递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?