Solved

multiple constructors

Posted on 1999-01-06
17
267 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:chandas
  • 5
  • 4
  • 2
  • +4
17 Comments
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
I believe that D4 allows this. The other Delphi versions do not allow this.
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
:-) 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
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
:-)

I think it took almost to years from Delphi3 to Delphi4, so I guess, Delphi5 will come in 1,5 years...
0
 

Author Comment

by:chandas
Comment Utility
Edited text of question
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
better clearefy what you exactly what to get. Maybe with a little example?
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
better clearefy what you exactly want to get. Maybe with a little example?
0
 

Expert Comment

by:tleforge
Comment Utility
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
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
tleforge,

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

Regards, Madshi.
0
 

Expert Comment

by:tleforge
Comment Utility
oops!!! sorry... next time I'll read the WHOLE message ;0)
0
 
LVL 8

Expert Comment

by:ZifNab
Comment Utility
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
 
LVL 3

Expert Comment

by:williams2
Comment Utility
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
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 50 total points
Comment Utility
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
 

Author Comment

by:chandas
Comment Utility
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
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now