|
|
|||
| H o m e | |||
|
|
I m p l e m e n t i n g C l a s s P r o p e r t i e s By Michael McIntyre
This article describes two techniques for implementing properties in your classes and the merits of each.
1. Properties as Class Fields
The simplest - but often incorrect - way to define properties is by adding public fields to the class statement. For example, you could create two properties in an Account class by declaring two public fields. Note: A data variable declared in a class statement, outside of the methods in the class, is known as a ‘field’ in object-oriented-programming.
NOTE: You can create private data for a class. Declare a field Private, and it will be accessible only from code within the class statement.
2. Properties as Property Statements
Property statements are the slightly harder - but most often correct – way to implement properties in a class.
Property statements provide a way to implement data hiding for a class. The ability to protect part of an object's data, while exposing the rest as properties, is called data hiding. This is one aspect of the object-oriented principle of encapsulation.
Data hiding means
that you can make changes in the implementation of a class — for example,
changing the Account class' private field
Data hiding isn’t possible when using public fields as class properties. How much good would it do you to use a public ‘Balance’ field as a class property, if any code that had a reference to an Account object could unsafely set the account balance to any value at all?
Property statements allow you to execute code when a property value is set or retrieved. For example, you might want to implement an AccountType property as a property statement that validates data being passed into a private field named _AccountType:
Class Account ' Define an account type enumeration. Public Enum AccountTypes savingsAccount = 1 checkingAccount LineOfCreditAccount End Enum
' AccountType field; private storage ' for the AccountType property. Private _AccountType As AccountTypes
' AccountType property statement; public interface ' for reading and modifying the _AccountType field. Public Property AccountType() As AccountTypes Get ' Return the current value of _AccountType. Return _AccountType End Get Set(ByVal Value As AccountTypes) ' Validate the Value passed in. Select Case Value Case AccountTypes.checkingAccount, _ AccountTypes.savingsAccount, _ AccountTypes.LineOfCreditAccount ' Value is valid account type; ' change value of _AccountType. Me.AccountType = Value Case Else ' Value is not valid account type; ' throws an exception. Throw New Exception("Invalid account type.") End Select End Set End Property
End Class
Now suppose you have
a variable named
When the code
In short, property statement Set methods allow an object to protect and validate its own data.
Data hiding also allows you to define properties that are read-only. The keyword ReadOnly is used and the Set method is ommitted from the property statement. For example, here is the AccountType property statement from the previous code example modifed to be read-only.
' AccountType property; public interface ' for reading the _AccountType field. Public ReadOnly Property AccountType() As AccountTypes Get ' Return the current value of _AccountType. Return _AccountType End Get End Property
Property Statements vs. Public Variables
Property statements are clearly such a powerful means for enabling encapsulation that you may be wondering if you should even bother with public variables. The answer, as always in programming, is "Of course — sometimes." Here are some ground rules:
Use property statements when:
· The property is read-only, or cannot be changed once it has been set. · The property has a well-defined set of values that need to be validated. · Values outside a certain range — for example, negative numbers — are valid for the property's data type, but cause program errors if the property is allowed to assume such values. · Setting the property causes some perceptible change in the object's state, as for example a Visible property. · Setting the property causes changes to other internal variables or to the values of other properties.
Use public variables for read-write properties where:
· The property is of a self-validating type. For example, an error or automatic data conversion will occur if a value other than True or False is assigned to a Boolean variable. · Any value in the range supported by the data type is valid. This will be true of many properties of type Single or Double. · The property is a String data type, and there's no constraint on the size or value of the string.
Click here to learn more about the Property statement.
|
||
|
Copyright © 2001-2005 aZ Software Developers. All rights reserved. |
|||