Home >>VB.Net Built-in Functions >VB.Net Program to demonstrate the MyBase keyword

VB.Net Program to demonstrate the MyBase keyword

Write a VB.Net Program to demonstrate the MyBase keyword

We will create a Sample1 base class here and then inherit the Sample1 class into the Sample2 class. Then we can use the MyBase keyword to name the base class method.

Program :

The source code to demonstrate the keyword of MyBase is given below. The program given is compiled and successfully executed.

'VB.net program to demonstrate the MyBase keyword.


Module Module1
    Class Sample1
        Sub Fun1()
            Console.WriteLine("Sample1.Fun1() called")
        End Sub
    End Class
    Class Sample2
        Inherits Sample1
        Sub Fun2()
            MyBase.Fun1()
            Console.WriteLine("Sample2.Fun2() called")
        End Sub
    End Class
    Sub Main()
        Dim S As New Sample2()
        S.Fun2()
    End Sub
End Module
	
Output:
Sample1.Fun1() called
Sample2.Fun2() called
Explanation:

We created a Module1 module in the program above that comprises two classes, Sample1 and Sample2.

One process is found in both classes. Here, class Sample1 has been inherited into class Sample2. Then we used the MyBase keyword from the Sample2 class to pull up the base class method.

Finally, we created a Main() function, which is the program entry point, here we created the Sample2 class object, and called Fun2() which will call Sample1 class Fun1() method.


No Sidebar ads