Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call correctly base class constructor from inherited class in Delphi Phrism?

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,

like image 648
ThN Avatar asked Aug 29 '11 20:08

ThN


People also ask

What is Delphi inherited command?

Delphi Basics : Inherited command DelphiBasics Inherited Keyword Used to call the parent class constructor or destructor method 1 keyword Inherited(Create; begin Inherited; // Always call at the start of a constructor end; 2 Create(arguments); begin InheritedCreate(arguments); // Always call at the start of a constructor end; 3 Destroy;

Can we inherit base class constructors in the derived class?

This article is about the inheritance concept in C++ and how we can inherit the base class’ constructors in the derived class. In C++, particularly in object-oriented programming, the most fundamental and widely used concept is that of inheritance.

How do you call a constructor in a derived class?

With the help of base keyword, the derived class can call the constructor which is defined in its base class. Note: Any form of the constructor defined in the base class can be called by the base keyword, but only that constructor executes that matches the arguments.

How do you use inherited methods in constructor?

It is called at the start of a constructor, and at the end of a desctructor. It is not mandatory, but recommended as good practice. Without parameters, Inherited calls the same named method the parent class, with the same parameters. You can call a different parent method, if appropriate.


Video Answer


1 Answers

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

like image 183
Carlo Kok Avatar answered Nov 05 '22 10:11

Carlo Kok