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.
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
Create an Application That Contains a Visual Basic .NET or Visual Basic 2005 Class
- Start Visual Studio .NET or Visual Studio 2005, and create a new Visual Basic .NET or Visual Basic 2005 Console Application project named EmployeeProperties.
- 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.
- Examine the code for the new class in the Code View window.
Define Fields, Constructors, and Methods
- Add three Private fields to the Employee class, as follows:
Private Name As String
Private Salary As Decimal
Private HireDate As DateTime
- 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
- Add a Public method to the class, as follows:
Public Sub PayRise(ByVal Amount As Decimal)
Me.Salary += Amount
Define Properties in the Class
- 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
Set(ByVal Value As String)
Me.Name = Value
- 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
- 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
Dim timespan As TimeSpan = DateTime.Now.Subtract(Me.HireDate)
Create an Object and Use the Properties
- Display the code for Module1.vb in the Code View window.
- 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", _
New DateTime(1998, 7, 31) )
- Add the following code to change the employee FullName property. This implicitly invokes the Set accessor for the FullName property.
worker.FullName = "Jayne Lafayette"
- 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)
Test the Application
- Build and run the application.
- The application displays the following information in the console (the "Days since joined" value depends on the current date):
Full name: Jayne Lafayette
Days since joined: 1139
- 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
- Try to build the application. You receive a compilation error because Remuneration is a read-only property.
- Change the statement as follows:
- 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.
Article ID: 308230 - Last Review: December 6, 2006 - Revision: 3.3
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
|kbvs2005applies kbvs2005swept kbhowtomaster KB308230|