Creating PROPERTIES in VB.NET

In this article You will learn How to create properties in VB.NET.
  • 7862

In this article We will describe How to create PROPERTIES in VB.NET.

Creating PROPERTIES:

  1. A property is a special method having two section called get and set.

  2. They look like fields for outside world but internally methods.

  3. Use to work with values rather than actions.

  4. When using set use value as reserve word to collect the value.

The general syntax to create the properties in VB.NET.:

Public Property ChangeHeight() As Integer

Get

End Get

Set(ByVal Value As Integer)

End Set

End Property   

For example:

Module Module1

    Class employee ' created class called employee

        Private m_empid As Integer

        Private m_name As String

        Private m_basic As Integer

        Public Property EmpId() As Integer     'creating property called empid

            Get

                Return m_empid

            End Get

            Set(ByVal value As Integer)

                m_empid = value

            End Set

        End Property

        Public Property Name() As String            'creating property called name

            Get

                Return m_name

            End Get

            Set(ByVal value As String)

                m_name = value

            End Set

        End Property

        Public Property Basic() As Integer             'creating property called basic 

            Get

                Return m_basic

            End Get

            Set(ByVal value As Integer)

                m_basic = value

            End Set

        End Property

        Public ReadOnly Property Salary() As Double       'creating property called salary readonly type

            Get

                Return m_basic * 2.4

            End Get

        End Property

 

    End Class

 

    Class PropertyTest

        Public Shared Sub Main()

            Dim e As New Employee()

            e.Name = "Amit Kumar"

            e.Basic = 9000

            System.Console.WriteLine("Salary of {0} is {1}", e.Name, e.Salary)

        End Sub

    End Class

End Module

 
 

OUTPUT:

 

property.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.