Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Why isn't this considered a proper overload?

This topic is 2469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!