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.
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
- Visual Basic .NET or Visual Basic 2005 syntax
- Object-oriented concepts
- 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.
- 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
- 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)
- 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)
- 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:
worker.Salary = 1000000
- 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: Dec 6, 2006 - Revision: 1