VB.NET Friend access modifier

Friend is default access modifier for that class that does not have an access modifier.
  • 3896
 

Friend is default access modifier for that class that does not have an access modifier. This access modifier is accessible only within the program that contains the entity declaration. External classes from your project cannot access this modifier.

Class1.vb

Public Class Class1
    Friend number As Integer

End
Class

Public Class Class2
    Public Sub CheckMethod()
        Dim x As New Class1()
        x.number = 65
    End Sub

End
Class
 

Class3.vb

Public Class Class3
    Public Sub OtherMethod()
        Dim x As New Class1()
        x.number = 78
        Console.WriteLine()
        Console.WriteLine(x.number)
    End Sub

End
Class

Module1.vb

Module Module1
    Sub Main()
        Dim x As New Class3()
        x.OtherMethod()
        Console.ReadLine()
    End Sub

End
Module

Output

output.gif
 

If you try to assign integer value to x then you will get an error. See below figure2.

Public Class Class3
    Public Sub OtherMethod()
        Dim x As New Class1()
        x = 78
        Console.WriteLine(x.number)
    End Sub

End
Class

Output

error.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.