Properties

  • Properties are members of classes that expose member variables or objects. Properties have similarities to both fields and methods.
  • Values are set and retrieved using the same syntax as fields: getting a value from a property or setting a value with a property actually calls a specialized method that carries out these functions. Properties can contain code that validates value before setting them or carries out any other function required by the application.
  • Properties allow you to expose member values and objects in a more robust way than simply using fields.
  • A property is essentially a specialized method that looks like a field
Creating a Property procedure

  • In this you declare the variables as Private
  • Use Get and Set property accessor methods to get and set the values of these variables
  • You also can index properties by passing an index value when referring to a property

  • Module Module1
    Private PropertyValue As Integer
    Public Property prop1() As Integer
    Get
    End Get
    Set(ByVal value As Integer)
    End Set
    End Property

  • Note: When you type Public Property prop1() As Integer and press Enter key then the skeleton for Get and Setaccessor method will be supplied by VB.NET. Value is the special keyword used in the setter of a property. It always represents the value to which the property is being set.
Example:
Module Module1
Sub Main()
Module2.Prop1 = 2
System.Console.WriteLine("Prop1=" & Module2.prop1)
End Sub
End Module
Module Module2
Private PropertyValue As Integer
Public Property prop1() As Integer
Get
Return PropertyValue
End Get
Set(ByVal value As Integer)
PropertyValue = value
End Set
End Property
End Module

Example:Using Index Property
Module Module1
Private Data(200) As Integer
Public Property Property1(ByVal Index As Integer) As Integer
Get
Return Data(Index)
End Get
Set(ByVal value As Integer)
Data(Index) = value
End Set
End Property
End Module
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.Property1(5) = 1
MsgBox(Module1.Property1(5))
End Sub
End Class
Public Class Form1
Public ob1 As New class1()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Module1.Property1(5) = 1
MsgBox(Module1.Property1(5))
ob1.Property1(2) = 55
MsgBox(ob1.Property1(2)) End Sub
End Class
Public Class class1
Private Data(200) As Integer
Public Property Property1(ByVal Index As Integer) As Integer
Get
Return Data(Index)
End Get
Set(ByVal value As Integer)
Data(Index) = value
End Set
End Property
End Class

Creating Class(Shared) Properties
  • Like a Data member and Method, a property can also be shared. It can be done with the key word shared
  • In a shared property, you can only work with shared data, or data passed by you.
Public Class Form1
Public Class mathematics
Shared data As Integer = 0
Shared Property property1()
Get
Return data
End Get
Set(ByVal value)
data = value
End Set
End Property
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
mathematics.property1 = 7
MessageBox.Show("Property Set")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = mathematics.property1
End Sub


Hashtable << Previous

Next>>Types of properties

Our aim is to provide information to the knowledge seekers. 


comments powered by Disqus
Footer1