Sign in to follow this  
Alpha_ProgDes

Why isn't this considered a proper overload?

Recommended Posts

I get this error: [quote]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
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:

[code]
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)[/code]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this