Classes in Visual FoxPro
Classes define properties, methods, and events, which are characteristics and functionality that objects possess and that you can use to manipulate objects. Classes can also contain other objects or data members such as variables. When an object, or instance, is created from a class, it contains the properties, methods, events, and any other objects or members as defined by the class.
The properties that a class defines specify characteristics for an object of that class. For example, the CommandButton class has a Name property, which defines the name of the command button as opposed to the Caption property, which defines the text that the command button displays to the user.
The methods of a class define and store procedure code that runs when you call the method for an object of that class. For example, the CommandButton class has a Move method, which moves the command button to coordinates that you specify.
The events of a class define code that executes when the system or user performs actions that trigger the events for an object of that class. For example, the CommandButton class contains a Click event, which is a procedure that you can use to define code that executes when the user clicks the command button.
Classes can also contain other objects, for example, a Form class can contain a control object, such as a command button. You can also specify data members, such as variables, as part of a class.
The following sections provide more information about Visual FoxPro classes:
Benefits of Using Classes
Types of Visual FoxPro Classes
For more information about objects in Visual FoxPro, see Working with Objects in Visual FoxPro.
Benefits of Using Classes
Classes provide the following features that make it possible for you to create reusable and more maintainable code:
Encapsulation
Encapsulation, which includes packaging property and method code into an object, hides unnecessary complexity and helps contribute to abstraction. Abstraction makes it possible for you to focus on aspects of the object that you want or need to use instead of the low-level details.
For example, when a person uses a computer, the person usually does not want to know how it operates, such as how it processes data or maintains files. The person wants to accomplish tasks, such as write a letter, and does not need to know how those tasks are performed.
Likewise, a list box class encapsulates properties controlling how items display in the list box and functionality determining the actions performed when choosing items in the list box into a single control.
Subclass creation
For more information, see Subclasses in Visual FoxPro.
Inheritance
For more information, see Subclasses in Visual FoxPro.
These features help you create and maintain code more easily and quickly.
Types of Visual FoxPro Classes
Visual FoxPro classes, and by extension, objects, fall into two primary types: container classes and control classes. Classes can also be visual or non-visual.
Container Classes and Control Classes
Containers can consist of other objects, such as controls, and provide access to the objects they contain. Control classes are more encapsulated than container classes; however, for that reason, they can be less flexible. Control classes do not have an AddObject Method.
For example, suppose you create a container class that consists of two list boxes and two command buttons. You then add an object based on the container class to a form. You can manipulate each object in the container at design time and run time; for example, you can change the positions of the list boxes or the captions of the command buttons. You can also add other objects to the container at design time; for example, you can add labels to identify the list boxes.
The following table lists examples of objects that can exist in container classes.
Container |
Can contain |
---|---|
Column |
Headers and any objects except form sets, forms, toolbars, timers, and other columns |
CommandGroup |
Command buttons |
Container |
Any controls |
Control |
Any controls |
Custom |
Any controls, data environment, page frame, container, custom |
DataEnvironment |
Cursors, relations, and cursor adapters |
FormSet |
Forms, toolbars |
Form |
Page frames, data environment, any controls, containers, custom |
Grid |
Columns |
OptionGroup |
Option buttons |
PageFrame |
Pages |
Page |
Any controls, containers, custom |
Project |
Files, servers |
Toolbar |
Any controls, page frame, container |
Visual and Non-Visual Classes
In Visual FoxPro, objects and controls created from visual classes display a visual element at design time and run time. Objects and controls based on non-visual classes, such as the Custom class and Timer control, display a visual element only at design time but not at run time.
See Also
Tasks
How to: Create Classes and Subclasses