[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

multiple constructors

Posted on 1999-01-06
17
Medium Priority
?
276 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +4
17 Comments
 
LVL 8

Expert Comment

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

Expert Comment

by:Madshi
ID: 1354921
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
ID: 1354922
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:ZifNab
ID: 1354923
:-) 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
ID: 1354924
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
ID: 1354925
:-)

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
ID: 1354926
Edited text of question
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354927
better clearefy what you exactly what to get. Maybe with a little example?
0
 
LVL 8

Expert Comment

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

Expert Comment

by:tleforge
ID: 1354929
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
ID: 1354930
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
ID: 1354931
oops!!! sorry... next time I'll read the WHOLE message ;0)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1354932
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
ID: 1354933
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 150 total points
ID: 1354934
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
ID: 1354935
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
ID: 1354936
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

650 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