THIS Object Reference

Provides a reference to the current object in event code or in a class definition.

THIS.PropertyName | ObjectName

Parameters

  • PropertyName
    Specifies a property to set or get for the object.
  • ObjectName
    Specifies an object in the class.

Remarks

THIS provides a convenient way of referring to the current object when writing event-handling programs in a form. For example, this Click Event program for a command button sets the button's caption to the current time:

this.caption = time()

Using THIS instead of explicitly referring to the current object by name (for example, thisform.command1.caption) makes program code portable between objects, because it avoids the object's name, and automatically encapsulates the object's parent class.

THIS also allows you to reference a property or an object in a class definition. Methods in a class definition block can use THIS to specify a property or object that will exist when the class is created.

Because multiple instances of objects share the same method code, THIS always refers to the instance in which the code is executing. If there are multiple instances of an object, and one of the object's methods is called, THIS refers to the correct object.

Example

The following example creates a subclass called MyForm, based on the Form class. A method called ChangeBackColor is created. ChangeBackColor uses THIS to refer to MyForm.

DEFINE CLASS MyForm AS FORM
 CAPTION = "This Form"
 HEIGHT = 15
 WIDTH = 20

 PROCEDURE ChangeBackColor
 PARAMETER NewColor
  THIS.BACKCOLOR = NewColor
 ENDPROC
ENDDEFINE

See Also

Reference

THISFORM Object Reference
THISFORMSET Object Reference
BackColor, ForeColor Properties
DEFINE CLASS Command

Other Resources

Objects, Collections, and Classes
Language Reference (Visual FoxPro)