Recent

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

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 323
  • https://www.pustertal.org/de/pustertal/unteres-pus
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 »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

Zvoni

  • Hero Member
  • *****
  • Posts: 803
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 323
  • https://www.pustertal.org/de/pustertal/unteres-pus
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 :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

Zvoni

  • Hero Member
  • *****
  • Posts: 803
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 IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

wp

  • Hero Member
  • *****
  • Posts: 9013
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.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 323
  • https://www.pustertal.org/de/pustertal/unteres-pus
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 :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018