Public Class Student
Public Overridable Sub Test()
Console.WriteLine("Hello, I am a student.")
End Sub
End Class
Public Class Undergraduate
Inherits Student
Public Overrides Sub Test()
MyBase.Test ' skip this if this class COMPLETELY
' replaces the base class functionality
Console.WriteLine("...and I am an undergrad.")
End Sub
End Class
Dim a As New Student
Dim b As New Undergraduate
a.Test
b.Test
Output:
Hello, I am a student ' from a
Hello, I am a student ' from b's MyBase.Test
...and I am an undergrad. ' from b
Your question relates to late binding. By passing o as Object
, the IDE cannot tell whether the Hello method is available on a given object. It might be and it might not be. If you have Option Strict On
the default settings will not allow this to compile for just this reason. If you are sure you know what you are doing, you can customize the error to change Late Binding errors to warnings or even ignore them.
The restructured code may give a better view of using inherited classes to avoid needing to use late binding and because passing an object to itself is a little odd.