Microsoft KB Archive/308230

= How to define and use properties in Visual Basic .NET or in Visual Basic 2005 =

Article ID: 308230

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q308230



For a Microsoft Visual C# .NET version of this article, see 319265.



IN THIS TASK
SUMMARY
 * Requirements
 * Create an Application That Contains a Visual Basic .NET or Visual Basic 2005 Class
 * Define Fields, Constructors, and Methods
 * Define Properties in the Class
 * Create an Object and Use the Properties
 * Test the Application



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.
 * 1) Examine the code for the new class in the Code View window.

back to the top

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 End Sub   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
  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   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 </li>  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 </li></ol>

back to the top

Create an Object and Use the Properties
 Display the code for Module1.vb in the Code View window.</li>  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( &quot;Jayne Doe&quot;, _                            125000, _                             New DateTime(1998, 7, 31) ) </li>  Add the following code to change the employee FullName property. This implicitly invokes the Set accessor for the FullName property. worker.FullName = &quot;Jayne Lafayette&quot; </li>  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(&quot;Full name: &quot; & worker.FullName) Console.WriteLine(&quot;Remuneration: &quot; & worker.Remuneration) Console.WriteLine(&quot;Days since joined: &quot; & worker.DaysSinceJoined) </li></ol>

back to the top

Test the Application
 Build and run the application.</li> The application displays the following information in the console (the &quot;Days since joined&quot; value depends on the current date):

<pre class="fixed_text">Full name: Jayne Lafayette Remuneration: 125000 Days since joined: 1139

</li>  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 </li> Try to build the application. You receive a compilation error because Remuneration is a read-only property.</li>  Change the statement as follows: worker.Salary = 1000000 </li> 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.</li></ol>

back to the top

Keywords: kbvs2005applies kbvs2005swept kbhowtomaster KB308230

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.