Overloaded Published Properties lazarus / 21-General ---------------------------------------------------------------------- SandyG · 2024-06-06 04:04 Working on keeping existing code compatible as I make some changes, ran into the notion that it might be possible to overload a property so that the existing code that uses the component might not need any modification. The code I'm working on is in the BGRAComponents, ThemedGauge. This is the signature of the exiting property Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     property Position: integer read GetPosition write SetPosition; Want to change it to Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     property Position: single read GetPosition write SetPosition; It's no problem to set the value of the float property with an integer as this is pascal doing it type conversions, but not using it as a getter, some simple code I have kind of like this will cause a compiler error (rightfully so if property is not overloading)... Again not sure if even possible to overload a property or how! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Var   someInt : integer; begin  someInt := themedGauge.Position; // compile error, yes Round(themedGauge.Position) would work but require code changes for users of the component...  I tried to Overload and create additional setters and getters which compiled just fine, but in the test program (like above) it will not compile. Class where I do this looks like this after adding Overloaded code (compiles fine), but didn't really expect it to work since their seems to be no way to make a property overloaded (??)  and only the single type is declared- Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TDTCustomThemedGauge = class(TDTBaseAnalogDevice)  private...    FPosition: single;     procedure SetPosition(AValue: single); overload;    procedure SetPosition(AValue: integer); overload;    function GetPosition : single; overload;    function GetPosition : integer; overload;...  protected    property Position: single read GetPosition write SetPosition; // Not sure what to do here or how to make this overloaded...  end;   { TDTThemedGauge }  // this is then used to create a themed gauge  TDTThemedGauge = class(TDTCustomThemedGauge)  private  protected    { Protected declarations }  public    { Public declarations }  published    { Published declarations }    ...    property Position;  // no idea how OR if this can be overloaded, I'm guessing no???  end;  Compiling with FPC 3.2.2 Not a huge deal to get this working, just thought I would see if anyone has tried something similar to keep the user from having to modify their code, but not a huge change if they do (add Round() when reading the prop in their code). Thanks Sandy ---------------------------------------------------------------------- lainz · 2024-06-06 12:10 Just Add PositionFloat and Position. Manage the code as always using float, but display as well as integer... ---------------------------------------------------------------------- jamie · 2024-06-06 21:27 You can do just about anything with a Variant Property ! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    fvalue :Variant;    Procedure fSetter(Const A:variant);  public    Property Test:Variant Read fvalue write fSetter;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin  Test := 1.2340;  Test := 1;  Test :='Test';end; Procedure TForm1.fSetter(Const A:Variant);Begin  fValue := A;end; end. ---------------------------------------------------------------------- lainz · 2024-06-06 22:29 Quote from: jamie on June 06, 2024, 11:27:26 pmYou can do just about anything with a Variant Property ! Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    fvalue :Variant;    Procedure fSetter(Const A:variant);  public    Property Test:Variant Read fvalue write fSetter;  end; var  Form1: TForm1; implementation {$R *.lfm} procedure TForm1.Button1Click(Sender: TObject);begin  Test := 1.2340;  Test := 1;  Test :='Test';end; Procedure TForm1.fSetter(Const A:Variant);Begin  fValue := A;end; end.   And you can read the variable Test as integer and as float, even if it was assigned to float only? ---------------------------------------------------------------------- jamie · 2024-06-06 22:38 Yes you can. It simply truncates it to an integer; If you want a float then simply assign it to a float..  Its all about the magic that takes place in the background!  Of course there are limitations to everything, it can't read my back account because they don't make number systems small enough! ---------------------------------------------------------------------- lainz · 2024-06-06 22:44 Quote from: jamie on June 07, 2024, 12:38:10 amYes you can. It simply truncates it to an integer; Awesome, thanks! ---------------------------------------------------------------------- SandyG · 2024-06-06 23:36 That might be a good way to do it. Will have to check in the setter for Valid data, but should not be too hard. Really was trying to avoid the variant, but does what I need and pretty simple. Thanks! Sandy ---------------------------------------------------------------------- jamie · 2024-06-07 00:35 Checking the data type is tricky, let us know if you need assistance. P.S.   I am not really sure how a variant is streamed from the resource file, it could be translated to a real type on the fly. ---------------------------------------------------------------------- SandyG · 2024-06-07 05:19 I gave it a try and it seems to work fine. The odd thing is that in the properties editor in Lazarus it asks for a 'type' but it's always set to double. That being said, in the code the assignment to single, float, or integer works OK. The .lfm file only has a value, not a type stored from what I see. It's a little odd to see this in the property editor. Still have to try it with a replacement of an existing gauge in a fresh project, but I think it will work. More testing for sure, but close. Thanks again!! Sandy