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

VB.Net Program to demonstrate the 'Friend' keyword

Write a VB.Net Program to demonstrate the 'Friend' keyword

Here, with the Friend keyword, we can create a subroutine or procedure. The Friend keyword is used to limit the scope that can not be reached outside the same assembly by a method, Friend method or subroutine.

Program :

Below is the source code to demonstrate the Friend keyword. The program given is compiled and successfully executed.

'VB.net program to demonstrate the "Friend" keyword.

Class Sample
    ' The Fun() is public if we are in the same assembly.
    Friend Sub Fun()
        Console.WriteLine("Friend function Fun() called")
    End Sub
End Class
Module Module1
    Sub Main()
        Dim obj As New Sample()
    End Sub
End Module
Friend function Fun() called

We built a class sample in the above program that includes a Friend method, which can only be accessed within the same assembly.

After that, we built a module Module1 that contains the Main() method, and the application entry point is the Main() method. And, we created the Sample class object and then called the Fun() method, which will print the appropriate message on the screen of the console.

No Sidebar ads