Recent

Author Topic: Object Property as Procedure variable  (Read 675 times)

ge.vasiliou

  • New Member
  • *
  • Posts: 10
Object Property as Procedure variable
« on: March 07, 2020, 11:21:10 pm »
Hello,
I want to make a kind of data validation procedure, in which i want to pass the text stored in a TextBox and also pass the property that needs to be set of a particular object.

Example:

Code: Pascal  [Select][+][-]
  1. procedure ValidateEntry(Sender1 :TObject; Sender2:TObject);
  2. var
  3. TheBox: TEdit;
  4. TheLabel: TLabel;
  5. DefaultValue: Integer;
  6. begin
  7.    TheBox:= Sender1 as TEdit;
  8.    TheLabel:=Sender2 as TLabel;
  9.    DefaultValue:=100;
  10.    if TheBox.Text<>'' then TheLabel.Left:=StrToInt(TheBox.Text) else TheLabel.Left:=DefaultValue;
  11. end;
  12.  
Above procedure works ok and can be called by main program using something like
Code: Pascal  [Select][+][-]
  1. ValidateEntry(Edit1,Label1)

But i would like to be possible to call the same procedure ValidateEntry by passing also the Label1 property that needs to be adjusted every time

For example i have imagined something like
Code: Pascal  [Select][+][-]
  1. ValidateEntry(Edit2,Label1,Width) //to adjust the Label1.Width property based  on the Edit5.Text value.
  2. ValidateEntry(Edit3,Label1,Height) //to adjust Label1.Height , and so on

But i can not figure out how to write the code inside ValidateEntry procedure in order to pass and handle the required property like TheLabel.property_passed:=......

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Object Property as Procedure variable
« Reply #1 on: March 07, 2020, 11:34:44 pm »
Hi!

You don't have to pass height and width as an extra parameter.

TheLabel1width and TheLabel1.height already contain this values.
Just change them.

Winni
« Last Edit: March 07, 2020, 11:46:44 pm by winni »

ge.vasiliou

  • New Member
  • *
  • Posts: 10
Re: Object Property as Procedure variable
« Reply #2 on: March 08, 2020, 12:12:10 am »
Hi Winni,
Thank you for your reply.
Consider that in my real project i have 20 TEdits that are going to change 20 different properties of an Object (in this post example is a TLabel).

I want to avoid to write 20 times the same code like
if EditXX.Text<>'' then do_some_things else do_other_things;

I am thinking of a global data validation procedure that can be called to validate any TEdit data.

My example posted here is simple enough just to give you the idea of what i am trying to achieve. Reallity is much more complicated.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Object Property as Procedure variable
« Reply #3 on: March 08, 2020, 12:18:37 am »
You can use RTTI and pass the name of the property as string.

Code: Pascal  [Select][+][-]
  1. procedure Validate(Sender1, Sender2: TComponent; PropertyName: String);

Bart

ge.vasiliou

  • New Member
  • *
  • Posts: 10
Re: Object Property as Procedure variable
« Reply #4 on: March 08, 2020, 12:25:30 am »
Hi @Bart,
Very interesting, thanks for the tip.
I was not aware about the existance of RTTI.

Can this task achieved without using RTTI Controls?

« Last Edit: March 08, 2020, 02:14:52 am by ge.vasiliou »

 

TinyPortal © 2005-2018