DEFINE CLASS Command

Example   See Also

Creates a user-defined class or subclass and specifies the properties, events, and methods for the class or subclass.

Syntax

DEFINE CLASS ClassName1 AS ParentClass [OLEPUBLIC]
  [[PROTECTED | HIDDEN PropertyName1, PropertyName2 ...]
     [Object.]PropertyName = eExpression ...]
  [ADD OBJECT [PROTECTED] ObjectName AS ClassName2 [NOINIT]
     [WITH cPropertylist]]...
  [[PROTECTED | HIDDEN] FUNCTION | PROCEDURE Name[_ACCESS | _ASSIGN]
  | THIS_ACCESS [NODEFAULT]
     cStatements
  [ENDFUNC | ENDPROC]]...
ENDDEFINE

Arguments

ClassName1

Specifies the name of the class to create.

AS ParentClass

Specifies the parent class on which a class or subclass is based. The parent class can be a Visual FoxPro base class, such as the Form class, or another user-defined class or subclass.

The following table lists the Visual FoxPro base classes:

Base class names

ActiveDoc ProjectHook
CheckBox Label
Column Line
ComboBox ListBox
CommandButton OLEControl
CommandGroup OLEBoundControl
Container OptionButton
Control OptionGroup
Cursor Page
Custom PageFrame
DataEnvironment Relation
EditBox Separator
Form Shape
FormSet Spinner
Grid TextBox
Header Timer
Hyperlink ToolBar
Image  

A non-visual user-defined class is created by specifying Custom for ParentClass.

In the following example, a subclass named MyForm is created, based on the Form base class. A Click method is created that displays a dialog box when MyForm is clicked.

DEFINE CLASS MyForm AS Form
   PROCEDURE Click
      = MESSAGEBOX('MyForm has been clicked!')
   ENDPROC
ENDDEFINE

OLEPUBLIC

Specifies that the class in an Automation server can be accessed by an Automation client.

If a program containing an OLEPUBLIC class definition is added to a project, an executable (.exe) file or a dynamic link library (.dll) containing the class can be created interactively in the Project Manager or with BUILD EXE or BUILD DLL. The EXE or DLL is automatically registered with the operating system, and becomes available to any Automation client.

For information about creating custom Automation servers, see Creating Automation Servers in Chapter 16, “Adding OLE.”

[PROTECTED | HIDDEN PropertyName1, PropertyName2 ...]
[Object.]PropertyName = eExpression ... Creates a class or subclass property and assigns a default value to the property. Properties are named attributes of the class and define characteristics and behaviors for the class. Classes and subclasses can have multiple properties.

Use = to assign a value to the property. The following example creates a user-defined class named MyClass and creates two properties called Name and Version. The Name property is initialized to the empty string and the Version property is initialized to the character string 1.0.

DEFINE CLASS MyClass AS Custom
   Name = ''
   Version = '1.0'
ENDDEFINE

A property can be accessed outside the class or subclass definition after the object is created with CREATEOBJECT( ):

MyOjbect = CREATEOBJECT('MyClass')

Properties are accessed with the following syntax:

ObjectName.Property

The .Object keyword indicates to Visual FoxPro that the property value should be applied when the ActiveX control is created.

The following example adds the Outline ActiveX control to a form. The Object keyword is used to specify a property for the Outline control before it is created.

PUBLIC frmOLETest
frmOLETest = CREATEOBJECT('Form')
frmOLETest.Visible = .T.

frmOLETest.ADDOBJECT('OCXTest', 'BlueOLEControl', ;
   'MSOutl.Outline')
frmOLETest.OCXTest.AddItem('Item One')
frmOLETest.OCXTest.AddItem('Item Two')

DEFINE CLASS BlueOLEControl AS OLEControl

   * Set a property of the ActiveX control
   .Object.Backcolor = 16776960
   
   * Set properties of the OLE Container Control
   Visible = .T.
   Height = 100
   Width = 200
ENDDEFINE

Include PROTECTED and a list of property names to prevent access and changes to the properties from outside of the class or subclass definition. Methods and events within the class or subclass definition can access the protected properties.

In the following example, the Version property is protected, preventing it from being accessed and changed outside of the class definition. However, the Name property is not protected and can be accessed and changed.

DEFINE CLASS MyClass AS Custom
   PROTECTED Version
   Name = ''
   Version = '1.0'
ENDDEFINE

Include HIDDEN and a list of property names to prevent access and changes to the properties from outside of the class definition. Only methods and events within the class definition can access the hidden properties. While protected properties can be accessed by subclasses of the class definition, hidden properties can only be accessed from with the class definition.

ADD OBJECT

Adds an object to a class or subclass definition from a Visual FoxPro base class, user-defined class or subclass, or ActiveX custom control.

PROTECTED

Prevents access and changes to the object's properties from outside the class or subclass definition. The PROTECTED keyword must be placed immediately before ObjectName or FoxPro generates a syntax error.

ObjectName

Specifies the name of the object and is used to reference the object from within the class or subclass definition after an object is created from the class or subclass definition.

AS ClassName2

Specifies the name of the class or subclass containing the object you add to the class definition. For example, the following class definition adds a command button from the CommandButton base class and a list box from the ListBox base class.

DEFINE CLASS MyClass AS Custom
   ADD OBJECT CB1 AS CommandButton
   ADD OBJECT LIST1 AS ListBox
ENDDEFINE

NOINIT

Specifies that an object's Init method is not executed when the object is added.

WITH cPropertyList

Specifies a list of properties and property values for the object you add to the class or subclass definition. For example, the following class definition creates a class called MyClass, adds a command button to the class definition, and specifies the Caption and BackColor properties for the command button.

DEFINE CLASS MyClass AS CUSTOM
   ADD OBJECT CB1 AS CommandButton;
      WITH Caption = 'Cancel', BackColor = 2
ENDDEFINE

FUNCTION | PROCEDURE Name[_ACCESS | _ASSIGN] | THIS_ACCESS

Create events and methods for the class or subclass. Events and methods are created as a set of functions or procedures.

You can create an event function or procedure within a class or subclass definition to respond to an event. An event is an action such as a mouse click that is recognized by an object created with a class or subclass definition. For additional information about Visual FoxPro event processing, see "The Core Events" in Chapter 4, Understanding the Event Model, in the Programmer's Guide.

Events are called with the following syntax:

ObjectName.Event

You can also create a method function or procedure within a class or subclass definition. A method is a procedure that acts upon the object created with the class or subclass definition. Methods are called with this syntax:

ObjectName.Method

The _ACCESS and _ASSIGN suffixes can be added to a procedure or function name to create an Access or Assign method for a property of the same name. The code in an Access method is executed whenever the property is queried. The code in an Assign method is executed whenever you attempt to change the value of the property.

In addition, you can create a THIS_ACCESS procedure or function that is executed whenever you attempt to change the value of a member of an object or a member of an object is queried.

For more information about creating Access and Assign methods with DEFINE CLASS, see Access and Assign Methods in Chapter 33, Programming Improvements, in the Programmer's Guide.

NODEFAULT

Prevents Visual FoxPro from performing its default event or method processing for Visual FoxPro events and methods. For example, if the KeyPress event occurs, including NODEFAULT in the KeyPress procedure or function prevents Visual FoxPro from placing the key press into the Visual FoxPro keyboard buffer. This allows you to create a KeyPress procedure that lets you test which key is pressed before the key is sent to the keyboard buffer.

NODEFAULT may be placed anywhere within the event or method procedure. Note that NODEFAULT may also be placed within an event or method procedure in the Form Designer.

cStatements
  [ENDFUNC | ENDPROC]]...
ENDDEFINE

cStatements are the Visual FoxPro commands that are executed when an event or method is executed.

Event and method functions and procedures can accept values by including a PARAMETERS or LPARAMETERS statement as the first executable line of the function or procedure.

Unlike most Visual FoxPro keywords, you cannot abbreviate ENDFUNC and ENDPROC. This prevents conflicts with the ENDFOR and ENDPRINTJOB keywords.

The following example demonstrates how to create an event procedure that displays a message when the command button is clicked. This event procedure overrides the default command button Click event.

DEFINE CLASS MyClass AS Custom
   ADD OBJECT MyButton AS CommandButton
   ADD OBJECT MyList AS ListBox
   PROCEDURE MyButton.Click
      = MESSAGEBOX('This is my click event procedure')
   ENDPROC
ENDDEFINE

Remarks

User-defined classes are a set of commands placed in a program file, similar to a procedure. The commands that follow the class or subclass definition define the properties, events, and methods for the class or subclass.

Note   You cannot have normal executable program code included in a program file after procedures; only class definitions, procedures, and user-defined functions can follow the first DEFINE CLASS, PROCEDURE or FUNCTION command in the file.

Class and subclass definitions created with DEFINE CLASS cannot be placed within structured programming commands, such as IF ... ENDIF or DO CASE ... ENDCASE. Nor can they be placed in loops, such as DO WHILE ... ENDDO or FOR ... ENDFOR.

To create an object from a class or subclass definition, issue CREATEOBJECT( ) with the class or subclass name.