I knew VB.net is very weird when talking about shadows and overloads, but this this I’m completely baffled.
I’m working with a model similar to the following one. Parent class:
Public Class Base
Function F() As String
Return "F() in Base Class"
End Function
Function F(ByVal n As Integer) As String
Return "F(" + n.ToString() + ") in Base Class"
End Function
End Class
and this:
Class Derived
Inherits Base
Shadows Function F() As String
Return "-"
End Function
End Class
when running the following:
Sub Main()
Dim parent As Base = New Base()
Dim child As Derived = New Derived()
Console.WriteLine(parent.F())
Console.WriteLine(parent.F(1))
Console.WriteLine("------------")
Console.WriteLine(child.F())
Console.WriteLine(child.F(1)) 'this should not compile, due to the shadow keyword.
Console.Read()
End Sub
an IndexOutOfRangeException is thrown. Moreover, when changing (in derived Class):
Return “-”
for
Return “Func in derived class”
console prints character ‘u’.
Does somebody knows the reason for this?
Your code is indexing a String rather than calling a function with a parameter.
This line is expanded to:
Because
String.Charsis the default property, you can reference it by index alone. Your string only contains one character, so there is no character at index 1.