7

I have two classes - base class and inherited class as follows.

Base Class:

TAlarm = class(System.Object)
private:
protected:
public:
    constructor (tag:TTagname);
end;

inherited class:

  TAlarmMsg = class(TAlarm)
  public
    constructor (aname:string);
    method GetAlarmMsg:string; override;
    method SendMsg(msg:string);
  end;

constructors:

constructor TAlarm(tag:TTagname);
begin
  Tagname := tag;
end;

constructor TAlarmMsg(aname:string);
begin
  inherited TAlarm(aname); <========Here is my problem.
  name := aname.ToCharArray;
end;

No matter what or how I call or play around with inherited constructor, I keep getting the following error messages when I compile the source file.

- Self cannot be accessed before the inherited constructor has finished. And/OR - Cannot find appropriate constructor in base class so manual call to inherited is required

By the way, I have spent good half a day researching on this issue and have found good information online. Nothing helps so far. I even found the webpage that directly talks about constructors on Delphi Prism Wikipedia ( http://prismwiki.embarcadero.com/en/Constructors ).

So, how would you do it correctly? Thanks,

ThN
  • 3,235
  • 3
  • 57
  • 115

1 Answers1

6

The statement inherited constructor(aName); should do it.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Carlo Kok
  • 1,128
  • 5
  • 14
  • @ CK, aName is a string not array of char and that's why it had problem. I simply corrected the offending line as follow: inherited constructor(aName.ToCharArray); It works. Thanks, – ThN Aug 29 '11 at 20:58
  • instruction `inherited;` alone at the beginning of the sub-class constructor seems to be ok – herve-guerin Mar 22 '17 at 12:04
  • Yep that works too. Although not when I answered this – Carlo Kok Mar 23 '17 at 08:47