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

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi for W...OOP   Print This     
  From the December 2015 Issue of Prestwood eMag
 
Delphi OOP:
Delphi Constructors (constructor)
 
Posted 16 years ago on 11/3/2008 and updated 8/18/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page

KB101488

General Info: Class Constructor

Constructors are called when you instantiate an object from a class. This is where you can initialize variables and put code you wish executed each time the class is created. When you initially set the member fields and properties of an object, you are initializing the state of the object. The state of an object is the values of all it's member fields and properties at a given time.

Languages Focus: Constructor

What is the syntax? Can you overload constructors? Is a special method name reserved for constructors?

Delphi Constructors

In Delphi, use the constructor keyword to signify which method or methods are constructors for a class. It is traditional but not required to use a procedure called Create.

In addition to having multiple named constructors, you can overload constructors.

Syntax Example:
//Interface section.
TCyborg = class(TObject)
public
  constructor Create;
end; 

Then implement the class constructor in the Implementation section.

constructor TCyborg.Create;
begin
  inherited;  //Call the parent Create method
end;

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.

Constructor and Destructor Example

SystemTime Class: In this final example, we want to create a class that will allow us to store the current time and then to restore it when we are finished with it.

Source Code for our class

unit SystemTime;  
interface
uses
   Windows, Messages, SysUtils, Classes, Graphics,
   Controls, Forms, Dialogs, StdCtrls;  
type
   TSysTime = class(TObject)
   private
     FStartDateTime : TDateTime;
   protected
   { Protected declarations }
   public
     constructor Create; virtual;
     destructor Destroy; override;
      procedure SetOldTime;
   published
   { Published declarations }
end;
implementation
constructor TSysTime.Create; 
begin
   FStartDateTime := Now;
end;
destructor TSysTime.Destroy;
var
   NewTime : TSystemTime;
begin
   DateTimeToSystemTime(FStartDateTime, NewTime);
   SetSystemTime(NewTime); 
end;  
procedure TSysTime.SetOldTime; 
var
   NewTime : TSystemTime;  
begin
   // set time to an arbitrary date/time
   NewTime.wYear := 1996;
   NewTime.wMonth := 1; 
   NewTime.wDayOfWeek    := 1;
   NewTime.wDay := 8;
   NewTime.wHour := 5;
   NewTime.wMinute  := 32;
   NewTime.wSecond  := 22;
   NewTime.wMilliseconds := 10;
   SetSystemTime(NewTime);
 end;
end.

More Info

Definition:  Class Constructor

Comments

0 Comments.
Share a thought or comment...
 
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 = P1116A1
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 play-drums.com 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


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Intermediate

2 Intermediate Level Questions

Question #1: Multiple Choice

The method name of a constructor is?

Answer:
1. 

Create

2. 

Not defined, use the constructor keyword to signify which method or methods are constructors for a class.

3. 

New

4. 

Same name as the class.

5. 

~ + class name, i.e. ~Person

Question #2: True or False?

The method name for a class constructor can be anything including Create and CreateFromTable.

Answer:
  • True
  • False

  •  KB Article #101488 Counter
    20725
    Since 11/3/2008
    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


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