How to define and use properties in Visual Basic .NET or in Visual Basic 2005 (308230)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q308230
For a Microsoft Visual C# .NET version of this article, see 319265.

SUMMARY

One important aim of many object-oriented programming languages is to allow you to encapsulate internal details in a class. Visual Basic .NET or Visual Basic 2005 allows you to define properties to provide controlled access to internal details in a class. This article describes how you can define and use properties in a Visual Basic .NET or Visual Basic 2005 application.

A property can be read-only, write-only, or read-write. A Get accessor provides read access, and a Set accessor provides write access. You can add code to these accessors to provide the appropriate functionality to get or set the property.

In this article, you create a class named Employee. Within this class, you define a read-write property for the name of the employee, a property to read the salary of the employee, and a property to calculate how long the employee has worked for the company. You then create an Employee object in your application and use its properties.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
  • Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
This article assumes that you are familiar with the following topics:
  • Visual Basic .NET or Visual Basic 2005 syntax
  • Object-oriented concepts
back to the top

Create an Application That Contains a Visual Basic .NET or Visual Basic 2005 Class

  1. Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic .NET or Visual Basic 2005 Console Application project named EmployeeProperties.
  2. On the Project menu, click Add Class. In the Add New Item dialog box, type Employee.vb in the Name text box, and then click Open.

    Note In Visual Studio 2005, click Add instead of Open.
  3. Examine the code for the new class in the Code View window.
back to the top

Define Fields, Constructors, and Methods

  1. Add three Private fields to the Employee class, as follows:
    Private Name As String
    Private Salary As Decimal
    Private HireDate As DateTime
    					
  2. Define a constructor for the class to initialize the fields in the Employee class, as follows:
    Public Sub New(ByVal Name As String, _
                   ByVal Salary As Decimal, _
                   ByVal HireDate As DateTime)
       Me.Name = Name
       Me.Salary = Salary
       Me.HireDate = HireDate
    End Sub
    					
  3. Add a Public method to the class, as follows:
    Public Sub PayRise(ByVal Amount As Decimal)
       Me.Salary += Amount
    End Sub
    					
back to the top

Define Properties in the Class

  1. Add the following code to add a read-write property named FullName. The Get accessor returns the name of the employee. The Set accessor uses the Value parameter that is passed into the Set accessor to set the name of the employee.
    Public Property FullName() As String
       Get
          Return Me.Name
       End Get
       Set(ByVal Value As String)
          Me.Name = Value
       End Set
    End Property
    					
  2. Add the following code to add a read-only property named Remuneration. The Get accessor returns the salary of the employee. There is no Set accessor because the salary cannot be set directly; the only way to change the salary is to call the PayRise method.
    Public ReadOnly Property Remuneration() As Decimal
       Get
          Return Me.Salary
       End Get
    End Property
    					
  3. Add the following code to add another read-only property named DaysSinceJoined. The Get accessor calculates how many days the employee has worked for the company.
    Public ReadOnly Property DaysSinceJoined() As Integer
       Get
          Dim timespan As TimeSpan = DateTime.Now.Subtract(Me.HireDate)
          return timespan.Days
       End Get
    End Property
    					
back to the top

Create an Object and Use the Properties

  1. Display the code for Module1.vb in the Code View window.
  2. In the Main method, use the New operator to create an Employee object. Assign the object reference to a local Employee variable, as follows:
    Dim worker As New Employee( "Jayne Doe", _
                                 125000, _
                                 New DateTime(1998, 7, 31) )
    					
  3. Add the following code to change the employee FullName property. This implicitly invokes the Set accessor for the FullName property.
    worker.FullName = "Jayne Lafayette"
    					
  4. Add the following code to display the details for the employee by using the Get accessors for the FullName, Remuneration, and DaysSinceJoined properties:
    Console.WriteLine("Full name: " & worker.FullName)
    Console.WriteLine("Remuneration: " & worker.Remuneration)
    Console.WriteLine("Days since joined: " & worker.DaysSinceJoined)
    					
back to the top

Test the Application

  1. Build and run the application.
  2. The application displays the following information in the console (the "Days since joined" value depends on the current date):

    Full name: Jayne Lafayette
    Remuneration: 125000
    Days since joined: 1139
    						

  3. Return to the Module1.vb file in the Code View window, and add the following code at the end of your Main method:
    worker.Remuneration = 1000000
    					
  4. Try to build the application. You receive a compilation error because Remuneration is a read-only property.
  5. Change the statement as follows:
    worker.Salary = 1000000
    					
  6. Try to build the application again. You receive another compilation error because the Salary field is marked as Private. The only way to modify the salary of the employee is to use the Public PayRise method.
back to the top

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005applies kbvs2005swept kbHOWTOmaster KB308230 kbAudDeveloper