How to Protect Properties and Methods in a ClassID: Q145894 3.00 3.00b WINDOWS kbprg kbhowto The information in this article applies to:
SUMMARYThis article shows how to use the PROTECT option of a class to hide properties and methods that are used only in the specific object class.
MORE INFORMATIONWithin Visual FoxPro, the PROTECTED keyword in the class definition prevents access to these properties by any methods outside of its class. Therefore, the PROTECTED properties are invisible to outside methods. The ability to hide properties or methods when using an object-oriented programming approach in Visual FoxPro is a form of encapsulation. The following example demonstrates the concept by creating a class called Protect_Globals. The PROTECTED keyword limits the scope of the property gd_sysdate to the Protect_Globals class. The property gc_day is not protected and can be accessed outside the class.
Create a Class with Protected and Unprotected Properties1. On the File menu, click New, select Class, and then click the New File 2. For the Class Name, enter Protect_Globals. This will be Based On Custom
3. Within the Class Designer, click the Class menu, and select New
4. On the Properties sheet, locate the gd_sysdate property, and change its
5. On the Class menu, click New Property. For the Name, type gc_day. Leave
6. On the Properties sheet, locate the gc_day property, and change its
7. On the Class menu, click New Method. For the Name, type getsysdate.
8. On the Properties sheet, locate the getsysdate method, and enter the
9. Close the Class Designer, and save changes to the Protect_Globals class.
Place the Class within a Form1. On the File menu, click New, and select Form, and then click the New 2. Select the View Classes tool from the Form Controls, and Add the g_class
3. Locate and select the Protect_Globals tool, and then click any place on
4. Select the View Classes tool from the Form Controls, and select
5. Add three command buttons (Command1, Command2, and Command3) to the new
6. Change the Caption of Command1 to GC_DAY. Within its Click event, enter
7. Change the Caption of Command2 to GETSYSDATE. Within its Click event,
8. Change the Caption of Command3 to GD_SYSDATE. Within its Click event,
9. Save the form as g_form, and close the Form Designer.
Run the Form1. Use the DO FORM command to run the g_form you just created. 2. Click the GC_DAY command button, and note that a WAIT WINDOW appears 3. Click the GETSYSDATE command button, and note that the gd_sysdate
4. Click the GD_SYSDATE command button. This results in a Program Error
5. Select Ignore, and close the form.
In Step 3 it was possible for the value to be displayed by calling a method of the Protect_Globals class that passed the data contained within the gd_sysdate property. But because that property is protected, it is completely hidden from the command button of the form that attempted to call the gd_sysdate property directly. Additional reference words: 3.00 3.00b Encapsulation VFoxWin KBCategory: kbprg kbhowto KBSubcategory: FxprgClassoop
|
Last Reviewed: February 13, 1996 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |