Recent

Author Topic: Can a form inherit from a superclass (non-form) ?  (Read 2029 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
Can a form inherit from a superclass (non-form) ?
« on: August 30, 2021, 09:00:26 am »
Hello :),

I Just wanted to ask how i could realize the following:

Superclass:
SuperC = class(TObject)

SubClass
MyForm = class(TForm) --> Should inherit from the "SuperC"

I have several forms, which share some properties. Therefore I wanted to create a superclass that initializes the variables of the shapes. Is it possible to do this with inheritance ? Since the form can't inherit from TForm and another class I don't know exactly which solution would be best here. Of course I could just manually assign the variables to the class each time, but this would not be such a nice solution in my eyes.

Maybe some tips from professionals? :)

########################################################################

Edit:
I realized that a class can derive from TForm even if it has no form itself. So now my "Data" class can initialize all forms. Should hopefully not give any peroblems later (haven't tested everything). If someone has a better solution I would be very happy if you could share it here. :)

Superclass:
TCars = class(TForm) --> Does not have a Form
TLocations = (LocGermany, LocRussia);

SubClass
TMyCar= class(TCars )
TMyTruck= class(TCars )
TMyMotorcycle= class(TCars )

Array of Components (Example)
Traffic : Array of TCars;

Traffic[1] := TMyCar.Create
Traffic[2] := TMyTruck.Create
Traffic[3] := TMyMotorcycle.Create

Traffic[1].SetGoal := LocGermany
Traffic[1].SetStart := LocRussia
Traffic[1].Drive();

Thanks
« Last Edit: August 30, 2021, 10:14:40 am by Weitentaaal »

Zvoni

  • Hero Member
  • *****
  • Posts: 2329
Re: Can a form inherit from a superclass (non-form) ?
« Reply #1 on: August 30, 2021, 10:14:39 am »
Yes, but why would you derive your Super-Class from TObject? TObject is the common Ancestor for all classes, even TForm
Derive it from TForm.
You can write your own constructor for your Superclass.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
Re: Can a form inherit from a superclass (non-form) ?
« Reply #2 on: August 30, 2021, 10:30:14 am »
Thanks Zvoni, but would my Example be a proper Solution ? or how should i approach something like this ?

Yes, but why would you derive your Super-Class from TObject? TObject is the common Ancestor for all classes, even TForm
Derive it from TForm.
You can write your own constructor for your Superclass.

"but why would you derive your Super-Class from TObject?"
I knew that TObject was the Ancestor of TForm but u have to write Something in there or don't u have to do that ?
TCars = class()  --> So i could just write this instead ?

"You can write your own constructor for your Superclass."
I did thanks a lot :)

Zvoni

  • Hero Member
  • *****
  • Posts: 2329
Re: Can a form inherit from a superclass (non-form) ?
« Reply #3 on: August 30, 2021, 11:02:42 am »
TCars = class()  --> So i could just write this instead ?
No, in that case it would inherit from TObject and not TForm.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

wp

  • Hero Member
  • *****
  • Posts: 11923
Re: Can a form inherit from a superclass (non-form) ?
« Reply #4 on: August 30, 2021, 11:07:24 am »
Put the common code into an ancestor of the forms used by your application:

- TBaseForm = class(TForm) --> all common code
- TMainForm = class(TBaseForm) --> all common code + code introduced by TMainform
- TSecondaryForm = class(TBaseForm) --> all common com + code introduced by TMainForm
- etc.

Weitentaaal

  • Hero Member
  • *****
  • Posts: 516
  • Weitental is a very beautiful garbage depot.
Re: Can a form inherit from a superclass (non-form) ?
« Reply #5 on: August 30, 2021, 12:34:35 pm »
Thanks for your effort, much appreciated. Everything working now :)

 

TinyPortal © 2005-2018