Share via


Protecting and Hiding Class Members

When you create and add properties and methods to a class in the Class Designer, you can specify one of the following levels of visibility for the property or method: Public, Protected, or Hidden. When you create a class using the DEFINE CLASS command, you can designate properties and methods as Hidden or Protected using the PROTECTED and HIDDEN keywords.

By default, properties and methods in a class definition are Public, which means code in other classes or procedures can set those properties or call those methods. However, certain classes might need to restrict users from changing its properties or calling its methods from outside the class. You can designate the properties and methods that you add to a class as Protected, which restricts access to members of the class and subclasses. You can also designate properties and methods as Hidden, which restricts access to only members of the class.

For example, suppose you create a class that stores employee information, and you do not want permit users to change the hire date for the employee. You can designate the hire date as Protected, and instead, provide a method that returns the hire date so that users can view the hire date if they need to. The following code example creates the Employee class using the DEFINE CLASS command and designates the HireDate property as Protected using the PROTECTED keyword and contains the method GetHireDate that returns the hire date:

DEFINE CLASS Employee AS CUSTOM
   PROTECTED HireDate
   First_Name = ""
   Last_Name = ""
   Address = ""
   HireDate = { - - }

   PROCEDURE GetHireDate
      RETURN This.HireDate
   ENDPROC
ENDDEFINE

For more information, see DEFINE CLASS Command.

The Visual FoxPro sample, Display a Stop Watch Sample, also illustrates the use of protected properties and methods in a class.

See Also

Tasks

How to: Add Properties to Classes

How to: Add Methods to Classes

Concepts

Classes in Visual FoxPro

Working with Classes in Visual FoxPro