Recent

Author Topic: Override a Property  (Read 1750 times)

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 323
  • Weitental is a very beautiful garbage depot.
Override a Property
« on: October 13, 2021, 01:13:48 pm »
Hello :),

Is it possible to override a Property ?
Have some inherited classes and a Property in Super class. Property reutrns a constant which is defined in Main Class. For every class the Property gets set by an Edit(Main Class). But for 1 Class it can be 2 different Values.

Normal Case

property P : Integer read GetP write SetP;
set := 300
get := 300

Other Case

property P : Integer read GetP write SetP;
can be 300 or 400

So is it possible to override it to -->

property P : Integer read GetP(MODE) write SetP(MODE);
set(True):= 300
set(False):= 400
Get(True):= 300
Get(False):= 400

I hope you understand what I mean :-[
I don't even know if Parameters in Property's are possible  :-[
(this is probably something you should never do like this, but im just thinking different  %))

thanks in advice :)

EDIT: Currently doing it like that:

Code: Pascal  [Select][+][-]
  1.  
  2. fValue1 :Integer;
  3. fValue2 :Integer;
  4.  
  5. property PValGeneral: Integer read GetVALG write SetVALG;
  6. property PVal1: integer read GetVal1 write SetVal1;
  7. property PVal2: integer read GetVal2 write SetVal2;
  8.  
  9. Get Valg
  10. IF MainForm.MODE = True then begin
  11.    Return := fValue1
  12. end else begin
  13.    Return := fValue2
  14. end;
  15.  
  16. Same for Setter
  17.  
  18. and for the other Setter/Getter i just do as usual
  19.  
  20.  
« Last Edit: October 13, 2021, 01:32:52 pm by Weitentaaal »
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 4511
    • Bart en Mariska's Webstek
Re: Override a Property
« Reply #1 on: October 13, 2021, 01:57:18 pm »
Can't you just declare the getter/setter as virtual and override them where necessary?

Bart

wp

  • Hero Member
  • *****
  • Posts: 9024
Re: Override a Property
« Reply #2 on: October 13, 2021, 02:24:37 pm »
Of course, properties can be indexed. You must use the "Index" identifier in the property declaration to indicate which value is to be used. How about this (untested):
Code: Pascal  [Select][+][-]
  1. type
  2.   TMode = (MODE1, MODE2);
  3.  
  4.   TBaseClass = class
  5.   private
  6.     FP: Integer;
  7.   protected
  8.     property P: Integer read FP write FP;
  9.   public
  10.   end;
  11.  
  12.   TDerivedClass1 = class(TBaseClass)
  13.   public
  14.     property P;
  15.   end;
  16.  
  17.   TDerivedClass2 = class(TBaseClass)
  18.   private
  19.     FOtherP: Integer;
  20.     function GetP(AMode: TMode): Integer;
  21.     procedure SetP(AMode: TMode; AValue: Integer);
  22.   public
  23.     property P1: Integer index MODE1 read GetP write SetP;
  24.     property P2: Integer index MODE2 read GetP write SetP;
  25.   end;
  26.  
  27. function TDerivedClass2.GetP(AMode: TMode): Integer;
  28. begin
  29.   case AMode of
  30.     MODE1: Result := inherited P;
  31.     MODE2: Result := FOtherP;
  32.   end;
  33. end;
  34.  
  35. procedure TDerivedClass2.SetP(AMode: TMode; AValue: Integer);
  36. begin
  37.   case AMode of
  38.     MODE1: inherited P := AValue;
  39.     MODE2: FOtherP := AValue;
  40.   end;
  41. end;
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

simone

  • Sr. Member
  • ****
  • Posts: 412
Re: Override a Property
« Reply #3 on: October 13, 2021, 05:07:58 pm »
You can find useful information on this topic in the following paragraph of the Language reference guide:

6.7.7 Overriding and redeclaring properties: https://www.freepascal.org/docs-html/ref/refsu39.html
Microsoft Windows 10 64 bit - Lazarus 2.0.12

Weitentaaal

  • Sr. Member
  • ****
  • Posts: 323
  • Weitental is a very beautiful garbage depot.
Re: Override a Property
« Reply #4 on: October 14, 2021, 09:56:35 am »
Can't you just declare the getter/setter as virtual and override them where necessary?

Bart

I Have to take a look at virtual nad overriding Property's first but i guess it will be a Solution ! Thanks

You can find useful information on this topic in the following paragraph of the Language reference guide:

6.7.7 Overriding and redeclaring properties: https://www.freepascal.org/docs-html/ref/refsu39.html

So this is very welcome :)

Of course, properties can be indexed. You must use the "Index" identifier in the property declaration to indicate which value is to be used. How about this (untested):
Code: Pascal  [Select][+][-]
  1. type
  2.   TMode = (MODE1, MODE2);
  3.  
  4.   TBaseClass = class
  5.   private
  6.     FP: Integer;
  7.   protected
  8.     property P: Integer read FP write FP;
  9.   public
  10.   end;
  11.  
  12.   TDerivedClass1 = class(TBaseClass)
  13.   public
  14.     property P;
  15.   end;
  16.  
  17.   TDerivedClass2 = class(TBaseClass)
  18.   private
  19.     FOtherP: Integer;
  20.     function GetP(AMode: TMode): Integer;
  21.     procedure SetP(AMode: TMode; AValue: Integer);
  22.   public
  23.     property P1: Integer index MODE1 read GetP write SetP;
  24.     property P2: Integer index MODE2 read GetP write SetP;
  25.   end;
  26.  
  27. function TDerivedClass2.GetP(AMode: TMode): Integer;
  28. begin
  29.   case AMode of
  30.     MODE1: Result := inherited P;
  31.     MODE2: Result := FOtherP;
  32.   end;
  33. end;
  34.  
  35. procedure TDerivedClass2.SetP(AMode: TMode; AValue: Integer);
  36. begin
  37.   case AMode of
  38.     MODE1: inherited P := AValue;
  39.     MODE2: FOtherP := AValue;
  40.   end;
  41. end;

Looks great ! i think with your Reply's i will be able to find a Solution that fits !

Thanks to you Guys :)
Lazarus: 2.0.12 x86_64-win64-win32/win64
Compiler Version: 3.2.0

 

TinyPortal © 2005-2018