18.13. Encapsulating Data with Properties


It is generally desirable to designate the member variables of a class as Private. This means that only member methods of that class can access their value. You make member variables Private to support data hiding.Data hiding is part of the encapsulation of a class, and allows you to change how you store your data without breaking other classes that interact with your class.

Finalize

If you do control an unmanaged resource, it is best to explicitly free that resource as soon as you are done with it. Implicit control over this resource is provided with a finalize method, which will be called by the garbage collector when your object is destroyed.

   Protected Overrides Sub Finalize(  )     ' release non-managed resources     MyBase.Finalize(  )   End Sub 

It is not legal to call finalize explicitly. Finalize will be called by the garbage collector. If you do handle precious unmanaged resources (such as file handles) that you want to close and dispose of as quickly as possible, you ought to implement the IDisposable interface. (You will learn more about interfaces later in this chapter.)

The IDisposable interface requires you to create a method named Dispose which will be called by your clients.

If you provide a Dispose method, you should stop the garbage collector from calling your object's destructor. To stop the garbage collector, you call the Shared method GC.SuppressFinalize, passing in the Me reference for your object. Your Finalize method can then call your Dispose method. Thus, you might write:

 Public Class Testing    Implements IDisposable    Dim is_disposed As Boolean = False    Protected Sub Dispose(ByVal disposing As Boolean)       If Not is_disposed Then          If disposing Then             Console.WriteLine("Not in destructor, OK to reference other objects")          End If          ' perform cleanup for this object          Console.WriteLine("Disposing...")       End If       Me.is_disposed = True    End Sub    Public Sub Dispose(  ) Implements IDisposable.Dispose       Dispose(True)       'tell the GC not to finalize       GC.SuppressFinalize(Me)    End Sub    Protected Overrides Sub Finalize(  )       Dispose(False)       Console.WriteLine("In destructor.")    End Sub End Class 


The public members of your class constitute a contract between your class and clients of your class, (any object that interacts with your class is a client.) Your public methods promise that if the client calls them with the right parameters, they will perform the promised action. How your methods perform that object is not part of the public contract. That is up to your class.

Properties allow clients to access class state as if they were accessing member fields directly, while actually implementing that access through a class method.

This is ideal. The client wants direct access to the state of the object. The class designer, however, wants to hide the internal state of his class in private class fields, and provide indirect access through a method. The property provides both: the illusion of direct access for the client and the reality of indirect access for the class developer.

By decoupling the class state from the method that accesses that state, the designer is free to change the internal state of the object as needed, without breaking the client.

You create a property with this syntax:

 Property Identifier(  ) As Type   Get     Statements   End Get   Set(ByVal Value As Type)      Statements   End Set End Property 

For example:

    Private mHour As Integer    Property Hour(  ) As Integer       Get          Return mHour       End Get       Set(ByVal Value As Integer)          mHour = Value       End Set    End Property 

If you create the property in Visual Studio .NET, however, the editor will provide extensive help with the syntax. Once you type, for example:

 Property Minute as Integer 

the IDE will reformat your property properly:

 Property Minute(  ) As Integer    Get    End Get    Set(ByVal Value As Integer)    End Set  End Property 

18.13.1. The Get Accessor

The body of the Get accessor is similar to a class method that returns an object of the type of the property. In the example, the accessor for Hour is similar to a method that returns an Integer. It returns the value of the private member variable in which the value of the property has been stored:

 Get    Return mHour End Get 

In this example, the value of a private Integer member variable is returned, but you could just as easily retrieve an Integer value from a database, or compute it on the fly.

Whenever you reference the property (other than to assign to it), the Get accessor is invoked to read the value of the property:

 Dim time1 As New Time(currentTime) Dim theHour As Integer = time1.Hour 

18.13.2. The Set Accessor

The Set accessor has one parameter, Value, that represents the assigned value. That is, when you write:

 Minute = 5 

the compiler passes the value you are assigning (5) to the Set accessor as the Value parameter. You can then set the member variable to that value:

 mMinute = Value 

The advantage of this approach is that the client can interact with the properties directly, without sacrificing the data hiding and encapsulation sacrosanct in good object-oriented design.



Programming Visual Basic 2005
Programming Visual Basic 2005
ISBN: 0596009496
EAN: 2147483647
Year: 2006
Pages: 162
Authors: Jesse Liberty

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