VB.Net program to demonstrate the MyBase keyword

Here, we are going to demonstrate the MyBase keyword in VB.Net.
Submitted by Nidhi, on December 28, 2020 [Last updated : March 06, 2023]

MyBase keyword in VB.Net

Here, we will create a base class Sample1 and then inherit Sample1 class into Sample2 class. Then we will call the base class method using the MyBase keyword.

Program/Source Code:

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

VB.Net code to demonstrate the example of MyBase keyword

'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
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains two classes Sample1 and Sample2.

Both classes contain one method. Here, we inherited Sample1 class into Sample2 class. Then we called the base class method using the MyBase keyword from Sample2 class.

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

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.