Adding Properties to a Class
In this lesson, you will learn how to add properties to the class that you created in the previous lesson.
In an earlier lesson, Closer Look: Understanding Properties, Methods, and Events, you learned that all objects have attributes, and that properties represent attributes. In the previous lesson, you created a Persons class that represents a person; people have attributes such as name and age, so the Persons class needs properties to represent those attributes.
Properties can be added to a class in one of two ways: as a field, or as a property procedure. You can also determine how a property works by using the Public, ReadOnly, or WriteOnly modifiers for the property.
Fields and Property Procedures
Fields are really just public variables within a class that can be set or read from outside of the class. They are useful for properties that don't need to be validated—for example, a Boolean (True or False) value. In the case of the Persons class, you might have a Boolean property named Married that specifies whether a person is single or married. Since there are only two possible values, a field works well for this property.
To add a field to a class, the code would look like the following.
Public Married As Boolean
Most properties, however, are more complex than that—in most cases you will want to use a property procedure to add a property to a class. Property procedures have three parts: a declaration of a private variable to store the property value; a Get procedure that exposes the value; and a Set procedure that, as it sounds, sets the value.
For example, a property procedure for a Name property for the Persons class would look like the following.
Private nameValue As String
Public Property Name() As String
Get
Name = nameValue
End Get
Set(ByVal value As String)
nameValue = value
End Set
End Property
The first line of code declares a private String variable, nameValue, which will store the value of the property. The property procedure itself begins with Public Property and ends with End Property.
The Get procedure contains the code that will be executed when you want to read its value—for example, if you read the Persons.Name property, the code would return the value stored in the nameValue variable.
The Set procedure contains code used to assign a new value to the nameValue variable using a value passed to it as a value argument. For example, if you wrote the code Persons.Name = "John", the String value John would be passed as the value argument; the code in the Set procedure would then assign it to the NameValue variable for storage.
You might ask why you would go to all that trouble rather than use a field to represent the Name property. In the real world, there are certain rules for names—for example, names do not usually contain numbers. You could add code to the Set procedure to check the value argument and return an error if it contains numbers.
In the following procedure, you will add a field and three properties to the Persons class.
Try It!
To add properties to your class
Open the Persons project that you created in the previous lesson. If you did not save it, you will first need to go back to the previous lesson, Modeling a Real-World Object: Creating Your First Class, and complete the procedures in that lesson.
In Solution Explorer, select Persons.vb, and then on the View menu, choose Code.
Add the following declaration code below the Public Class Persons line.
Private firstNameValue As String Private middleNameValue As String Private lastNameValue As String Public Married As Boolean
Add the following property procedures below the declaration code.
Public Property FirstName() As String Get FirstName = firstNameValue End Get Set(ByVal value As String) firstNameValue = value End Set End Property Public Property MiddleName() As String Get MiddleName = middleNameValue End Get Set(ByVal value As String) middleNameValue = value End Set End Property Public Property LastName() As String Get LastName = lastNameValue End Get Set(ByVal value As String) lastNameValue = value End Set End Property
On the File menu, choose Save All to save your work.
Read-only and Write-only Properties
Sometimes a property is meant to be set once and never changed during the execution of your program. For example, a property representing an employee number should never change, so it could be read by another program but you would not allow that program to change its value.
The ReadOnly keyword is used to specify that a property value can be read but not modified. If you try to assign a value to a ReadOnly property, an error occurs in the Code Editor.
To create a read-only property, you would create a property procedure with a Get procedure but no Set procedure, as follows.
Private IDValue As Integer
ReadOnly Property ID() As Integer
Get
ID = IDValue
End Get
End Property
Likewise, the WriteOnly keyword allows a property value to be set but not read—for example, you would not allow a password property to be read by other programs. You might use that value to do things within your class, but you would want to keep it private.
To create a write-only property, you would create a property with a Set procedure but no Get procedure, as follows.
Private passwordValue As String
WriteOnly Property Password() As String
Set(ByVal value As String)
passwordValue = value
End Set
End Property
ReadOnly and WriteOnly property procedures are also useful when you want to take one property value and convert it to a different value. For example, consider a person's age. Unlike a name, age changes over time—if you assigned your age to a class and read it back a year later, it would be wrong.
In the Persons class, you could prevent this by adding two properties—a WriteOnlyBirthYear property that represents the year of your birth, which never changes, and a ReadOnlyAge property that returns a value by calculating the difference between the current year and your birth year.
Try It!
To add ReadOnly and WriteOnly properties to your class
Add the following declaration code below the other declarations at the top of the class module.
Private birthYearValue As Integer
Add the following property procedures below the declaration code.
WriteOnly Property BirthYear() As Integer Set(ByVal value As Integer) birthYearValue = value End Set End Property ReadOnly Property Age() As String Get Return My.Computer.Clock.LocalTime.Year - birthYearValue End Get End Property
On the File menu, choose Save All to save your work.
Next Steps
In this lesson, you learned about properties and the different ways to add them to your class. In the next lesson, you will learn how to add methods to your class so that it can perform actions.
Next Lesson: Adding Methods to a Class
See Also
Tasks
Modeling a Real-World Object: Creating Your First Class
Concepts
Property Procedures vs. Fields