Your full service technology partner! 
-Collapse +Expand
Search Delphi Group:

-Collapse +Expand Delphi To/From
-Collapse +Expand Delphi Store

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the October 2015 Issue of Prestwood eMag
Delphi OOP:
Delphi Class..Object (class..end..Create)
Posted 16 years ago on 10/24/2008 and updated 6/10/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page


Languages Focus: Class..Object

In short, a class is a data type, and an object is an instance of a class type. A class has methods (routines), properties (member variables), and a constructor. The current values of the properties is the current state of the object. The UML is one of the diagraming disciplines that allows you to document the various changing states of a series of objects.

Delphi Class..Object

Declare your class in the Interface section. Then implement the class in the Implementation section. To create an object instance, call the class constructor (usually named Create). Since Delphi does not have a garbage collector, you have to also free the object usually with either Free or FreeAndNil.

Syntax Example:
//Interface section:
TCyborg = class(TObject)
procedure IntroduceYourself;
//Implementation section;
procedure TCyborg.IntroduceYourself;
ShowMessage('Hi, I do not have a name yet.');
//Some event like a button click:
T1: TCyborg;
T1 := T1.Create;
  FreeAndNil(T1);      //Be sure to clean up!

Complete Example

Here is a simple example of a Delphi class in it's own Person.pas unit. Notice how you define the class in the interface section and then fulfill it in the implementation section.

unit Person;


type //Custom types including classes.
TPerson = class(TObject) //Start of class.
FName: String;
FAge: Integer;
constructor Create; virtual;
property Name: String read FName write FName;
property Age: Integer read FAge write FAge;
end; //End of class.


constructor TPerson.Create;
inherited; //Call the parent Create method

//Set defaults.
FName := 'unknown';
FAge := 0;
end; //End of constructor method.
end. //End of unit.

To use your new class, include the unit in your uses clause and instantiate the object in your code:


Here is the code from a button click event:

procedure TForm2.Button1Click(Sender: TObject);
  Lisa: TPerson;  //de clare object variable.
  Lisa := TPerson.Create;  //Create object from class.
  //Use it. Show defaults.
  ShowMessage(Lisa.Name + ' is ' + IntToStr(Lisa.Age));
  //Use it. Set member fields then show.
  Lisa.Name := 'Lisa';
  Lisa.Age := 34;
  ShowMessage(Lisa.Name + ' is ' + IntToStr(Lisa.Age));

Step by Step Example

The following is a step-by-step tutorial on creating your first Delphi class and does include an inheritance example. From this simple tutorial, you can then experiment with the various class inheritance features.


More Info


Share a thought or comment...
Comment 1 of 2

Okay, I'll be picky.  Shouldn't this sentence:

In short, a class is a data type, and an object is an instance of a class type.


In short, a class is a data type, and an object is an instance of a class.

You really haven't identified a "class type" as being something.  But you just defined a class, so I would argue "an instance of a class" not "an instance of a class type".

Maybe it's just me... Smile

Posted 15 years ago

Comment 2 of 2

It's just youBig Grin!

Posted 15 years ago
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1244A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101403 Counter
Since 10/24/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site:

©1995-2024 Prestwood IT Solutions.   [Security & Privacy]