Access modifiers in vb.net
Access modifiers define the access to class members from the same class and from other classes. The most common access keywords are:
Public: Allows access to the class member from any other class.
Access modifiers define the access to class members from the same class and from other classes. The most common access keywords are:
Public: Allows access to the class member from any other class.
Example:
Public Class class1
Public i As Integer
Public Function raghu() As Integer
Console.writeline(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
Output=10
Reason: Allows access to the class member from any other class.
Private: Allows access to the class member only in the same class.
Example:
Imports System
Public Class class1
Private i As Integer
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Module Module1
Sub main()
Dim a As New class1
a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
Output: Error (“'ConsoleApplication.class1.i' is not accessible in this context because it is 'Private'.”)
Reason: Allows access to the class member only in the same class.
Output: Error (“'ConsoleApplication.class1.i' is not accessible in this context because it is 'Private'.”)
Reason: Allows access to the class member only in the same class.
Conclusion:
Imports System
Public Class class1
Private i As Integer = 10
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
' a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
Output=10
Protected: Allows access to the class member only within the same
class and from inherited classes.
Example:
Imports System
Public Class class1
Protected i
As Integer
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
Output=error
Reason: Allows access to the class member only within the same class and from inherited classes.
Reason: Allows access to the class member only within the same class and from inherited classes.
Conclusion:
Imports System
Imports System
Public Class class1
Protected i
As Integer = 10
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
a.raghu()
Console.ReadLine()
End Sub
End Module
Output=10
Friend /Internal: Allows access to the class member only in the same assembly.
Example:
Imports System
Public Class class1
Friend i As Integer
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
Ouput: 10
Reason: Allows access to the class member only in the same assembly.
Reason: Allows access to the class member only in the same assembly.
Protected internal: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly.
Example:
Imports System
Public Class class1
Protected Friend i As Integer
Public Function raghu() As Integer
Console.WriteLine(i)
End Function
End Class
Module Module1
Sub main()
Dim a As New class1
a.i = 10
a.raghu()
Console.ReadLine()
End Sub
End Module
output:10
Reason: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly
Reason: Allows access to the class member only within the same class, from inherited classes, and other classes in the same assembly
Static: Indicates that the member can be called without first instantiating the class.
Static: Indicates that the member can be called without first instantiating the class.
1) Load into memory once (we can save memory)
2) Instance not required
0 comments:
Post a Comment