Three classes: TTntMemo
, TTntEdit
and TEdit
have a common ancestor - TCustomEdit
, but I can't use Color
and ShowHint
properties of TCustomEdit
because they are protected
and are reintroduced as public
only in TTntMemo
, TTntEdit
and TEdit
. I am not allowed to change any of these classes because they belong either to VCL or to widely used controls libraries.
Following code is a PITA because it has to repeat itself three times - one time for each type:
class procedure TCommon.ValidateEdit(edit: TCustomEdit; condition: Boolean;
failHint: WideString);
var m: TTntMemo;
te: TTntEdit;
e: TEdit;
begin
if edit is TTntMemo then begin
m := edit as TTntMemo;
if condition then begin
m.Color := clWindow;
m.Hint := '';
m.ShowHint := False;
end
else begin
m.Color := $AAAAFF;
m.Hint := failHint;
m.ShowHint := True;
end;
end
else
if edit is TTntEdit then begin
te := edit as TTntEdit;
if condition then begin
te.Color := clWindow;
te.Hint := '';
te.ShowHint := False;
end
else begin
te.Color := $AAAAFF;
te.Hint := failHint;
te.ShowHint := True;
end;
end;
if edit is TEdit then begin
e := edit as TEdit;
if condition then begin
e.Color := clWindow;
e.Hint := '';
e.ShowHint := False;
end
else begin
e.Color := $AAAAFF;
e.Hint := failHint;
e.ShowHint := True;
end;
end;
end;
Unfortunately Delphi6 doesn't have reflection.
Do you have some ideas how this code could be optimized?
Use a hacked class of
TCustomEdit
or you can use
TypInfo
unit andUPDATE
Because all of the properties are declared in
TControl
you can also use this as your base class instead ofTCustomEdit
Suggestion to get very DRY
If I would implement such a validator, I would prefer to use a function to get back the
ACondition
valueIn my form I would use this (and it will become very DRY)