Why isn't this considered a proper overload?

Started by
5 comments, last by Concentrate 12 years, 8 months ago
I get this error:
cannot overload each other because they differ only by optional parameters.[/quote]

One function has 3 parameters and the other has 4. What am I missing here? (VB.Net 3.5)

[source lang="vb"]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[/source]

Beginner in Game Development?  Read here. And read here.

 

Advertisement
My reply may be totally worthless because I have no experience at all with VB, but the error message, despite my lack of experience in this particular language, conveys a very clear picture of what's most likely wrong: it cannot overload the functions when they differ only in optional parameters. Which of the two overloaded functions are called if you call the function with three string parameters?
I don't know VB, but it looks like you don't need to use the Overloads keyword for the first function of a given name that you define. So, the second one is a proper overload but the first one doesn't need the keyword.
I don't know VB much either, but what Bob said seems to be the case.

I'd suggest that you not overload, but just make the newName on the second function optional as well, check if its' been used when the function initiates and execute accordingly inside of the function, rather than overloading.

Interestingly enough, I don't even see a use for newName in the second function.... I see no reason for overloading at all, the functions are exactly the same.

Thanks guys. And yeah, I haven't finished writing the function. newName is going to be used. :)

Beginner in Game Development?  Read here. And read here.

 

Compare the two:


Protected Overloads Function updateResxNodes(ByVal keyCtrl As String, ByVal newValue As String, Optional ByVal newComment As String = "") As List(Of ResXDataNode)
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)


Say we've got a hypothetical call to updateResxNodes ("blah1", "blah"). That's unambiguous, it's the first one. Now say we need to make a call to updateResxNodes ("blah1", "blah", "blah3"). Which function gets called? The compiler knows that it's a function called updateResxNodes, it knows that you're passing it 3 strings, but which of the two overloads is the correct one to use?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Simple example to show the problem, in psuedo

void foo(int i = 0);
void foo();
///...

//call foo
foo(); //which one gets called? both definition can match this call
Edge cases will show your design flaws in your code!
Visit my site
Visit my FaceBook
Visit my github

This topic is closed to new replies.

Advertisement