Share via


Class Member Protection and Hiding

Properties and methods in a class definition are Public by default: code in other classes or procedures can set the properties or call the methods. Properties and methods that you designate as Protected can be accessed only by other methods in the class definition or in subclasses of the class. Properties and methods designated as Hidden can be accessed only by other members in the class definition. Subclasses of the class cannot "see" or reference hidden members.

To ensure correct functioning in some classes, you need to prevent users from programmatically changing the properties or calling the method from outside the class.

The following example illustrates using protected properties and methods in a class.

The stopwatch class included in Samples.vcx, in the Visual FoxPro ...\Samples\Classes directory, includes a timer and five labels to display the elapsed time:

The Stopwatch class contains labels and a timer.

Property Settings for the Stopwatch Class

Control Property Setting
lblSeconds Caption 00
lblColon1 Caption :
lblMinutes Caption 00
lblColon2 Caption :
lblHours Caption 00
tmrSWatch Interval 1000

This class also has three protected properties, nSec, nMin, and nHour, and one protected method, UpdateDisplay. The other three custom methods in the class, Start, Stop, and Reset, are not protected.

Tip   Choose Class Info on the Class menu to see the visibility of all properties and methods of a class.

The protected properties are used in internal calculations in the UpdateDisplay method and the Timer event. The UpdateDisplay method sets the captions of the labels to reflect the elapsed time.

The UpdateDisplay Method

Code Comments
cSecDisplay = ALLTRIM(STR(THIS.nSec))
cMinDisplay = ALLTRIM(STR(THIS.nMin))
cHourDisplay = ALLTRIM(STR(THIS.nHour))
Convert the numeric properties to Character type for display in the label captions.
THIS.lblSeconds.Caption = ;
 IIF(THIS.nSec < 10, ;
   "0" ,"") + cSecDisplay
THIS.lblMinutes.Caption = ;
  IIF(THIS.nMin < 10, ;
   "0", "") + cMinDisplay
THIS.lblHours.Caption = ;
  IIF(THIS.nHour < 10, ;
   "0", "") + cHourDisplay
Set the label captions, retaining the leading 0 if the value of the numeric property is less than 10.

The following table lists the code in the tmrSWatch.Timer event:

The Timer Event

Code Comments
THIS.Parent.nSec = THIS.Parent.nSec + 1
IF THIS.Parent.nSec = 60
  THIS.Parent.nSec = 0
  THIS.Parent.nMin = ;
  THIS.Parent.nMin + 1
ENDIF
Increment the nSec property every time the timer event fires: every second.
If nSec has reached 60, reset it to 0 and increment the nMin property.
IF THIS.Parent.nMin = 60
  THIS.Parent.nMin = 0
  THIS.Parent.nHour = ;
  THIS.Parent.nHour + 1
ENDIF
THIS.Parent.UpdateDisplay
If nMin has reached 60, reset it to 0 and increment the nHour property.

Call the UpdateDisplay method when the new property values are set.

The stopwatch class has three methods that are not protected: Start, Stop, and Reset. A user can call these methods directly to control the stopwatch.

The Start method contains the following line of code:

THIS.tmrSWatch.Enabled = .T.

The Stop method contains the following line of code:

THIS.tmrSWatch.Enabled = .F.

The Reset method sets the protected properties to zero and calls the protected method:

THIS.nSec = 0
THIS.nMin = 0
THIS.nHour = 0
THIS.UpdateDisplay

The user cannot directly set these properties or call this method, but code in the Reset method can.

Specifying the Default Value for a Property

When you create a new property, the default setting is false (.F.). To specify a different default setting for a property, use the Properties window. In the Other tab, click on your property and set it to the desired value. This will be the initial property setting when the class is added to a form or form set.

You can also set any of the base class properties in the Class Designer. When an object based on the class is added to the form, the object reflects your property settings rather than the Visual FoxPro base class property settings.

Tip   If you want to make the default setting of a property an empty string, select the setting in the Property Editing box and press the BACKSPACE key.

See Also

Object-Oriented Programming | Classes and Objects: The Building Blocks of Applications | Classes in Visual FoxPro | Preparation for Class Creation | Creating Classes | Modifying a Class Definition | Subclassing a Class Definition | Operating the Class Designer | Specifying Design-Time Appearance for a Class | Creating, Copying, and Removing Class Library Files | Adding Classes to Forms | Default Property Setting Override | Container Hierarchy Object Referencing | Setting Properties | Calling Methods | Event Response