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 was 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) 

There are several reasons why you might want to use property procedures rather than a public variable. 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 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 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net