PROPERTY PROCEDURES

Property procedures are routines that can represent a variable-like value. To other pieces of the program, property procedures look just like variables, so they deserve mention in this chapter.

The following code shows property procedures that implement a Name property. The Property Get procedure simply returns the value in the private variable m_Name. The Property Set procedure saves a new value in the m_Name variable.

Private m_Name As String
 
Property Name() As String
    Get
        Return m_Name
    End Get
    Set(ByVal Value As String)
        m_Name = Value
    End Set
End Property

A program could use these procedures exactly as if there were a single public Name variable. For example, if this code is in the Employee class, the following code shows how a program could set and then get the Name value for the Employee object named emp:

emp.Name = "Rod Stephens"
MessageBox.Show(emp.Name)

You might want to use property procedures rather than a public variable for several reasons. First, the routines give you extra control over the getting and setting of the value. For example, you could use code to validate the value before saving it in the variable. The code could verify that a postal code or phone number has the proper format and throw an error if the value is badly formatted.

You can also set breakpoints in property procedures. Suppose that your program is crashing because a piece of code is setting an incorrect value in a variable. If you implement the variable with property procedures, you can set a breakpoint in the Property Set procedure and stop whenever the program sets the value. This can help you find the problem relatively quickly.

Property procedures let you set and get values in formats other than those you want to actually use to store the value. For example, the following code defines Name property procedures that save a name in m_FirstName and m_LastName variables. If your code would often need to use the last and first names separately, you could also provide property procedures to give access to those values separately.

Private m_LastName As String
Private m_FirstName As String
             
Property MyName() As String
    Get
        Return m_FirstName & " " & m_LastName
    End Get
    Set(ByVal Value As String)
        m_FirstName = Value.Split(" "c)(0)
        m_LastName = Value.Split(" "c)(1)
    End Set
End Property

Finally, you can use property procedures to create read-only and write-only variables. The following code shows how to make a read-only NumEmployees property procedure and a write-only NumCustomers property procedure. (Write-only property procedures are unusual but legal.)

Public ReadOnly Property NumEmployees() As Integer
    Get
         ...
    End Get
End Property
 
Public WriteOnly Property NumCustomers() As Integer
    Set(ByVal Value As Integer)
        ...
    End Set
End Property

You don’t need to remember all of the syntax for property procedures. If you type the first line and press Enter, Visual Basic fills in the rest of the empty property procedures. If you use the keyword ReadOnly or WriteOnly, Visual Basic only includes the appropriate procedure.

Visual Basic also allows you to make auto-implemented properties. These are simply properties that do not have separate property procedures. You declare the property’s name, and Visual Basic automatically creates the necessary backing variables and property procedures behind the scenes.

The following code shows a simple FirstName property:

Public Property FirstName As String

You can give a property a default value as in the following code:

Public Property FirstName As String = "<missing>"

You cannot use the ReadOnly or WriteOnly keywords with auto-implemented properties. If you want to make a read-only or write-only property, you need to write Get and Set procedures as described earlier.

The advantage of auto-implemented properties is that you don’t need to write as much code. The disadvantage is that you can’t set breakpoints in the property procedures.


PROPERTY PROCEDURES AS YOU NEED THEM
To get the best of both worlds, you can initially use auto-implemented properties. Later if you need to set breakpoints in the property procedures, you can redefine the property to include them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.117