Member Access Modifiers and their effect

  • Public: can be accessed from anywhere
  • Private: Can be accessed only by members within the type that defines it
  • Friend: Can be accessed only by members within the assembly, but not from outside the assembly
  • Protected: Can be accessed only by members within the type that defines it or types that inherit from that type
  • Protected Friend: Can be accessed from all types within the assembly or from types inheriting from the owning type. This is the combination of Protected and Friend access modifier
Shared Memebers
  • It is possible to have members that are common to all instances of a class. These members are called Shared members. Only one instance of a Shared member can exist, no matter how many instances of a particular type have been created.
  • You can create shared member variable by using the Shared keyword. Shared members can still be Public, Private, Friend etc.
Example:

Public Shared i As Integer
  • Like member variables the methods can also be shared. So these methods belong to the type. Because shared methods belong to the type itself, they can not access instance data from any objects. They can only utilize shared variables, variables declared within the method, or parameters passed into the method.
Accessing Shared Memebers
  • Shared members are accessed using the class name unlike using the instances of an object because they belong to the class and not to the instances of the class.
  • Although VB.NET allows you to access Shared members through the object, there will still be only one instance of the shared members.
Public Class Form1 Public ob1 As New AddNum()
Public ob2 As New AddNum()
Public Class AddNum
Private j As Integer
Public Shared i As Integer = 1
Shared Function Add(ByVal a As Integer, ByVal b As
Integer) As Integer
Dim k As Integer = 66 'can use its member
'j=65 can not use because j is an instance member
MessageBox.Show(i, "From shared Function")
MessageBox.Show(k, "From shared Function")
Return a + b
End Function
Public Sub disp()
j = 55
i = i + 1
MessageBox.Show(i, "From Object")
MessageBox.Show(j, "From Object")
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
TextBox3.Text = AddNum.Add(TextBox1.Text, TextBox2.Text)
MessageBox.Show(AddNum.i, "Direct Access")
ob1.disp()
ob2.disp()
MessageBox.Show(AddNum.i, "Direct Access")
End Sub
End Class

Constructors and Destructors << Previous
Next >> Features of VB.NET

Our aim is to provide information to the knowledge


comments powered by Disqus




Footer1