multiple constructors

Thanx for all the help. Unfortunately I'm still stuck with Delphi 2, does this mean I have to actually give each constructor of my object a different name? Thus when calling the constructor, does this mean I must know before hand exactly which constructor I'm invoking? It's seems a bit messy, but kindly advise me on the neatest possible way to implement an object with more than one constructor in Delphi 2.
chandasAsked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
Here is a class with more than one constructor that works with D2: It is the only way of doing it (and hence the neatest, I suppose) in D2.

type
  TMyClass = Class(TObject);
    Constructor Create1(arg1, arg2, string);
    Constructor Create2(arg1, arg2, string);
  end;

Cheers,

Raymond.
0
 
ZifNabCommented:
I believe that D4 allows this. The other Delphi versions do not allow this.
0
 
MadshiCommented:
Hmm. Do you want to access either one or another constructor, or do you just want to override an inherited constructor?
In the first case, you'll need Delphi4, where you can write something like this:

type
        T1 = class(TObject)
          procedure Test(I: Integer); overload; virtual;
        end;
        T2 = class(T1)
          procedure Test(S: string); reintroduce; overload;
        end;
         ...
        SomeObject := T2.Create;
        SomeObject.Test('Hello!');  // calls T2.Test
        SomeObject.Test(7);         // calls T1.Test

This example works only with Delphi4!

If you just want to override a constructor, you can write
  constructor Create(p1: TBlaBla); override;

Regards, Madshi.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MadshiCommented:
Hey Zif, when will you (finally) get Delphi4?

In the meanwhile it's really stable and it's worth it's costs (I think)...    :-)
0
 
ZifNabCommented:
:-) Hey Madshi, thanks for letting me know it's stable... that's already a start to get it from the shop... but now i'm thinking... when will D5 come out :-)?
0
 
StefaanCommented:
Zifnab,

I think it will be approx 11 months before D6 and 3 months after VB7.  That makes it still worth waiting a while ;-)

0
 
MadshiCommented:
:-)

I think it took almost to years from Delphi3 to Delphi4, so I guess, Delphi5 will come in 1,5 years...
0
 
chandasAuthor Commented:
Edited text of question
0
 
ZifNabCommented:
better clearefy what you exactly what to get. Maybe with a little example?
0
 
ZifNabCommented:
better clearefy what you exactly want to get. Maybe with a little example?
0
 
tleforgeCommented:
Consider the following unit:

//**** Code begins ********************************************
unit MyButton;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TMyButton = class(TButton)
  private
    { Private declarations }
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent); overload; override;
    constructor Create(AOwner: TComponent; MyCaption: string); overload; virtual;
  published
    { Published declarations }
  end;

procedure Register;

implementation

constructor TMyButton.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 185;
  Height := 105;
end;

constructor TMyButton.Create(AOwner: TComponent; MyCaption: string);
begin
  inherited Create(AOwner);
  Width := 185;
  Height := 105;
  Caption := MyCaption;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TMyButton]);
end;

end.

//**** Code Ends ********************************************

This is a simple button I created as an example of using multiple constructors in Delphi 4.0 or greater.

It is possible to do this with most Delphi components (including forms).

Please note the order of the overload, override and virtual statements in the declarations of the constructors.  If they are not in the write order the compiler will scream at you.  In the end you will probably still get a warning... just ignore it.

Following is a test unit with one button on a form. That button creates another button of type TMyButton.

//**** Code begins ********************************************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MyButton;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  MyButton: TMyButton;
begin
  MyButton := TMyButton.Create(Self, 'asdf');
  MyButton.Parent := Self;
end;

end.
//**** Code ends ********************************************

Notice the create statement now reads:

  MyButton := TMyButton.Create(Self, 'asdf');

Run the example like this, but then change the create to:

  MyButton := TMyButton.Create(Self);

Presto!  I love Delphi... it makes everything almost too easy.
Hope this helps.  If you have any questions please feel free to email me at  tleforge@hotmail.com .

P.S.  Make sure you have the unit MyButton in the uses clause of the test form.
0
 
MadshiCommented:
tleforge,

your code is nice, but since it uses "overload" it works only with Delphi4, and chandas has only Delphi2...  :-(

Regards, Madshi.
0
 
tleforgeCommented:
oops!!! sorry... next time I'll read the WHOLE message ;0)
0
 
ZifNabCommented:
tleforge, chandas doesn't has D4, he has D2... (but I guess you didn't saw the previous comments, while working on this example)
0
 
williams2Commented:
You cannot have multiple constructors named like .Create .Delphi 2 does not allow polymorphismic overloads like in C++. Though I might ask you why you need this so bad?

Though I have some interest in the READ/WRITE command sets because it has the features of overloading.

Cheers,
Williams
0
 
chandasAuthor Commented:
Well thanx RWilson, you're right, that is one way to do it and it might even be the only way to do it since I'm handicapped with a lower version of Delphi...having come from Java recently I still find this construction a bit frustrating but what can I do? Anyway I was starting to think along the lines of a hierarchy of objects each with an overridden constructor (and hence all similarly named) so that perhaps I could place them in a container of a base class type at will. You've answered my question pretty well but what do you think about that? Overridden constructors?
Thanx again
0
 
rwilson032697Commented:
Overriden constructors are fine for creating objects in set of classes using polymorphism but you are still limited to having the same parameters. Otherwise you are just redefining the constructor (and so you still only get one of them with the same name accessible from outside the class).

If you want constructors differentiated by parameters in any version of Delphi prior to D4 you have to give them separate names.

Cheers,

Raymond.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.