karl Jonson 0 Posted July 20, 2022 (edited) Hi, I'm using a procedure which uses a TLabel parameter & sets the caption of the label: prcedure GetString(param1: string; param2:string; myLabel: TLabel); Begin myLabel.caption := GetDescription() End Everything works fine & I don't want to break it as it's called from different places. Now I'd like to be able to sometimes pass it TMemo and display multiple lines in the TMemo component: myMemo.lines.Add(GetDescription()); I'd like to be able to say if component is TLabel then do this but if it's TMemo then do that. TIA Edited July 20, 2022 by karl Jonson explanation Share this post Link to post
Uwe Raabe 2057 Posted July 20, 2022 procedure GetString(param1: string; param2:string; myLabel: TLabel); overload; Begin myLabel.caption := GetDescription(); End; procedure GetString(param1: string; param2:string; myMemo: TMemo); overload; Begin myMemo.lines.Add(GetDescription()); End; 3 Share this post Link to post
limelect 48 Posted July 20, 2022 (edited) If I understand your need add a flag to GetDescription(FLAG: boolean) so your function will know where it came from Memo.lines.add(getDescription(true)) or label.caption:=getdescription(false) Edited July 20, 2022 by limelect Share this post Link to post
Remy Lebeau 1394 Posted July 20, 2022 8 hours ago, karl Jonson said: Now I'd like to be able to sometimes pass it TMemo and display multiple lines in the TMemo component: myMemo.lines.Add(GetDescription()); I'd like to be able to say if component is TLabel then do this but if it's TMemo then do that. You can use the 'is' operator for that purpose, eg: procedure GetString(param1: string; param2: string; myControl: TControl); begin if myControl is TLabel then TLabel(myControl).Caption := GetDescription() else if myControl is TMemo then TMemo(myControl).Lines.Add(GetDescription()) else ...; end; Alternatively, every TControl has a Caption (and Text) property, even if it is not published. You can take advantage of that, eg: type TControlAccess = class(TControl) end; procedure GetString(param1: string; param2: string; myControl: TControl); begin if myControl is TMemo then TMemo(myControl).Lines.Add(GetDescription()) else TControlAccess(myControl).Caption := GetDescription(); end; Or, you can use RTTI instead, eg: procedure GetString(param1: string; param2: string; myControl: TControl); var Ctx: TRttiContext; Typ: TRttiType; Prop: TRttiProperty; begin Ctx := TRttiContext.Create; try Typ := Ctx.GetType(myControl.ClassType); Prop := Typ.GetProperty('Caption'); if (Prop <> nil) and (Prop.Visibility = mvPublished) then begin Prop.SetValue(myControl, GetDescription()); Exit; end; Prop := Typ.GetProperty('Text'); if (Prop <> nil) and (Prop.Visibility = mvPublished) then begin Prop.SetValue(myControl, GetDescription()); Exit; end; Prop := Typ.GetProperty('Lines'); if (Prop <> nil) and (Prop.Visibility = mvPublished) then begin (Prop.GetValue(myControl).AsObject() as TStrings).Add(GetDescription()); Exit; end; ... finally Ctx.Free; end; end; Share this post Link to post