Click here to Skip to main content
15,885,032 members
Please Sign up or sign in to vote.
4.82/5 (3 votes)
See more:
I have a class having two functions with same name and different type of arguments as described below:

VB
  Public Class functionOverload
'<--- first function starts here
        Public Function Test(ByVal x As Int16, ByVal y As Int16) As Double
            Return x * y
        End Function
'<--- first function Ends here

'<--- Second function starts here
        Public Function Test(ByVal x As Int32, ByVal y As Int32) As Integer
            Return x + y
        End Function
'<--- Second function ends here
    End Class

In my main module i had created an object of this class and call the function using different arguments:

VB
Dim obj As New functionOverload
 MsgBox(obj.Test(10, 20)) '<--- will call second function as both the parameters are of type int32
 Dim i, j As Int16
 i = 10
 j = 20
 MsgBox(obj.Test(i, j)) '<--- will call First function as both the parameters are of type int16


Up to this works fine as excepted. and i had tried even more as follows:

VB
MsgBox(obj.Test(i, 10)) '<--- will call second function even though their is no matching function
MsgBox(obj.Test(20, j)) '<--- will call second function even though their is no matching function


Question : why this is happening? Is the rules for function over loading is different in vb.net
Posted
Updated 29-Jan-15 1:11am
v2

In these cases;
VB
MsgBox(obj.Test(i, 10)) '<--- will call second function even though their is no matching function
MsgBox(obj.Test(20, j)) '<--- will call second function even though their is no matching function

It works because upcasting an Int16 to an Int32 is safe, so the compiler that do that, and then you have a call where you pass two Int32s (which is the second function) as the 10 and 20 will be interpreted as Int32s unless specified.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
Sujith Karivelil 29-Jan-15 7:13am    
+5 for this good answer. thanks for the solution
Fredrik Bornander 29-Jan-15 7:13am    
Glad I could help.
This really has more to do with VB.NET doing implicit conversions than it does with overloading.

The thing is that VB.NET will implicitly upconvert a value to the next wider type, but not convert anything down to a narrowing type unless explicitly told to do so. This is because a widening conversion has no chance of loosing data where a narrowing conversion can lose data.

This means that, unless told otherwise, VB.NET will chose a method where all of the value type either already match, or some of the parameters can be upconverted to wider types, then the method evaluation process starts all over again with the newly widened types. This continues until either a matching method is found or there are no other methods that can possibly match.

In other languages, you'd probably have to explicitly upconvert your values to get a matching type as not all languages will do implicit conversions for you. It's one of the gotchas that curly brace language people find annoying with VB.NET.
 
Share this answer
 
v2
Comments
Richard Deeming 29-Jan-15 14:36pm    
C# allows exactly the same implicit conversion and overload resolution, so this isn't a VB-specific behaviour.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900