I'm trying to figure out this weird behavior. I have a base class (A) with an overloaded method called "M": one for integers, and one for floats (single on VB.NET).
On the other hand, I have a second class (B) that inherits from A, and it overloads method M in two ways: one for data type double, and one for object data type.
The problem is: I expect the methods to use each function for its data type, but for some strange reason, the method with Object data type takes all the calls. Why is that happening?
I expected the output of this program to be 15
, but instead, it's 4
.
Here's the code (VB.NET):
Module Module1
Public Class A
Public Function M(ByVal a As Integer) As Integer
Return 8
End Function
Public Function M(ByVal a As Single) As Integer
Return 4
End Function
End Class
Public Class B
Inherits A
Public Overloads Function M(ByVal a As Double) As Integer
Return 2
End Function
Public Overloads Function M(ByVal a As Object) As Integer
Return 1
End Function
End Class
Sub Main()
Dim a0 As Double = 1
Dim a1 As Single = 2
Dim a2 As Integer = 4
Dim a3 As Object = 8
Dim arre(4)
arre(0) = a0
arre(1) = a1
arre(2) = a2
arre(3) = a3
Dim b As New B
Dim suma% = 0
For i = 0 To 3
suma += b.M(arre(i))
Next i
Console.WriteLine(suma)
System.Threading.Thread.CurrentThread.Sleep(2000)
End Sub
End Module