Show: Object Pascal C++
Display Preferences

Examining a Object Pascal Object

From Appmethod Topics
Jump to: navigation, search

Go Up to Using the object model Index

When you create a new project, the IDE displays a new form for you to customize. In the Code editor, the automatically generated unit declares a new class type for the form and includes the code that creates the new form instance. The generated code for a new Windows application looks like this:

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
TForm1 = class(TForm){ The type declaration of the form begins here }
  private
    { Private declarations }
  public
    { Public declarations }
  end;{ The type declaration of the form ends here }
var
  Form1: TForm1;
implementation{ Beginning of implementation part }
{$R *.dfm}
end.{ End of implementation part and unit}

The new class type is TForm1, and it is derived from type TForm, which is also a class.

A class is like a record in that they both contain data fields, but a class also contains methods - code that acts on the object's data. So far, TForm1 appears to contain no fields or methods, because you haven't added any components (the fields of the new object) to the form and you haven't created any event handlers (the methods of the new object). TForm1 does contain inherited fields and methods, even though you don't see them in the type declaration.

This variable declaration declares a variable named Form1 of the new type TForm1.

var
  Form1: TForm1;

Form1 represents an instance, or object, of the class type TForm1. You can declare more than one instance of a class type; you might want to do this, for example, to create multiple child windows in a Multiple Document Interface (MDI) application. Each instance maintains its own data, but all instances use the same code to execute methods.

Although you haven't added any components to the form or written any code, you already have a complete GUI application that you can compile and run. All it does is display a blank form.

Suppose you add a button component to this form and write an OnClick event handler that changes the color of the form when the user clicks the button. The result might look like this:

A simple form

Green.jpg

When the user clicks the button, the form's color changes to green. This is the event-handler code for the button's OnClick event:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Form1.Color := clGreen;
end;

Objects can contain other objects as data fields. Each time you place a component on a form, a new field appears in the form's type declaration. If you create the application described above and look at the code in the Code editor, this is what you see:

unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;
type
  TForm1 = class(TForm)
    Button1: TButton;{ New data field }
    procedure Button1Click(Sender: TObject);{ New method declaration }
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);{ The code of the new method }
begin
  Form1.Color := clGreen;
end;
end.

TForm1 has a Button1 field that corresponds to the button you added to the form. TButton is a class type, so Button1 refers to an object.

All the event handlers you write using the IDE are methods of the form object. Each time you create an event handler, a method is declared in the form object type. The TForm1 type now contains a new method, the Button1Click procedure, declared in the TForm1 type declaration. The code that implements the Button1Click method appears in the implementation part of the unit.

See Also

Personal tools
In other languages