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

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

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

   ► KBProgrammingDelphi PrismOOP   Print This     
  From the January 2016 Issue of Prestwood eMag
 
Prism OOP:
Delphi Prism Constructors (constructor + class name)
 
Posted 10 years ago on 1/18/2009 and updated 2/22/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101836



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 Prism Constructors

In Prism, a constructor is called whenever a class or struct is created. You use the constructor keyword with an unnamed method. You can overload the constructor simply by adding two or more unnamed methods with various parameters.

Prism also supports a Create constructor method for backward compatibility with Delphi for Win32.

If you do not create a constructor, Prism will create an implicit constructor that initializes all member fields to their default values.

Constructors can execute at two different times. Static constructors are executed by the CLR before any objects are instantiated. Regular constructors are executed when you create an object.

Syntax Example:
Cyborg = public class
public
  constructor();
  constructor(pName: String);
end;

Working WinForms Example

The following example demonstrates overloading a constructor so that you have the option to initialize a property at creation.

Create a form and place a button on it and alter the code as follows:

namespace CR_Constructor;
  
interface
  
uses
  System.Drawing,
  System.Collections,
  System.Collections.Generic,
  System.Linq,
  System.Windows.Forms,
  System.ComponentModel;
  
type
  /// <summary>
  /// Summary description for MainForm.
  /// </summary>
  MainForm = partial class(System.Windows.Forms.Form)
  private
    method button1_Click(sender: System.Object; e: System.EventArgs);
  protected
    method Dispose(disposing: Boolean); override;
  public
    constructor;
  end;
  
  Cyborg = class(System.Object)
  private
  public
    property CyborgName: String;  //Property using implicit syntax.
    constructor();                //No parems constructor.
    constructor(pName: String); //Constructor with a string parameter.
  end;
  
implementation
{$REGION Construction and Disposition}
constructor MainForm;
begin
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent();
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
  
method MainForm.Dispose(disposing: Boolean);
begin
  if disposing then begin
    if assigned(components) then
      components.Dispose();
    //
    // TODO: Add custom disposition code here
    //
  end;
  inherited Dispose(disposing);
end;
{$ENDREGION}
  
method MainForm.button1_Click(sender: System.Object; e: System.EventArgs);
var
  MyRobot1: Cyborg;
  MyRobot2: Cyborg;
begin
  MyRobot1 := New Cyborg;
  MyRobot1.CyborgName := "John";
  MessageBox.Show("My robot's name is " + MyRobot1.CyborgName + ".");
  
  MyRobot2 := New Cyborg("Cameron");
  MessageBox.Show("My robot's name is " + MyRobot2.CyborgName + ".");
end;
  
//Constructor with no parems.
constructor Cyborg();
begin
end;
  
//Constructor with a string parameter.
constructor Cyborg(pName: String);
begin
  CyborgName := pName;
end;
end.

Use the Legacy Create Constructor

If you turn on legacy support for Create, you can use the Delphi-style Create to instantiate objects instead of New:

var
MyRobot1: Cyborg;
begin
MyRobot1 := Cyborg.Create;
T1.CyborgName := "John";
  //...
end;

To turn on legacy support for Create, in the solution explorer right click on your solution or project and select Properties. On the Compatibility tab, check Allow 'Create' Constructor calls.

Allow Create constructor calls.

More Info

Definition:  Class Constructor
Code:  Delphi Prism Finalizer (finalize())

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 = P1261A1
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

1 Intermediate Level Question

Question #1: True or False?

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

Answer:
  • True
  • False

  •  KB Article #101836 Counter
    15556
    Since 1/18/2009
    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


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