Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Helper and Class Interceptor in Delphi XE2

I have a piece of code as shown bellow :

Interface section :

TControlOrganizer = class(TComponent)  // a kind of List of TControl
private
  FList : TList;  //Handling list of TControl
  ..............
end;

TControlHelperAdd = class // Just for separating value entered by user and value produced by code
  private
    FOrganizer : TControlOrganizer;
    FOrigValue : String;
    FUserValue : String;
end;

TControlHelper = class helper for TControl  //Basic helper for TControl
  private
    class var
      FAddProp : TControlHelperAdd;
  protected
    procedure SetOrganizer(Value : TControlOrganizer);
    function getOrganizer:TControlOrganizer;
  public
    class Constructor doinit;
    class Destructor deinit;
    procedure OrganizerChanged;virtual;
  published
    Property Organizer:TControlOrganizer read getOrganizer write setOrganizer; //Problem No.1
  end;

//Helper for CustomEdit inherited from TControlHelper
TCustomEditHelper = class helper(TControlHelper) for TCustomEdit
  protected
    procedure OrganizerChanged;override;
  end;
//TEdit : interceptor
TEdit= class(StdCtrls.TEdit)
  protected
    procedure Change;override;  //problem No.3
end;

implementation section :

{TControlHelper}

class Constructor TControlHelper.doinit;
begin
  FAddProp :=TControlHelperAdd.Create;
end;

class Destructor TControlHelper.deinit;
begin
  FreeAndNil(FAddProp);
end;

procedure TControlHelper.OrganizerChanged;
begin
end;

procedure TControlHelper.SetOrganizer(Value : TControlOrganizer);
begin
  if FAddProp.FOrganizer<>value then begin
     if assigned(FAddProp.FOrganizer) then begin
       FAddProp.FOrganizer.remove(self);
     end;
     FAddProp.FOrganizer:=value;
     if assigned(FAddProp.FOrganizer) then begin
       FAddProp.FOrganizer.Add(self);
     end;
     OrganizerChanged;  //Problem No.2
  end;
end;

function TControlHelper.getOrganizer:TControlOrganizer;
begin
  result:=FAddProp.FOrganizer;
end;

{TCustomEditHelper}
procedure TCustomEditHelper.OrganizerChanged; //problem No.2
begin
  if assigned(FAddProp) then begin
    if assigned(Organizer) then begin
      FAddProp.FOrigValue:=Text;
      FAddProp.FUserValue:=Text;
    end;
  end;
end;

{TEdit = Interceptor} 
procedure TEdit.Change; //Problem No.3
begin
  inherited Change;
  FAddProp.FUserValue:=Text;
end;

I found 3 problems that i couldn't solve myself.

Problem 1 : Published property property does not show up in the Object Inspector. I think I made a mistake and forgot something here.

problem 2 : overridden method from class helper as shown above didn't fire (didn't work).

problem 3 : overridden method from interceptor class as shown above didn't fire (didn't work).

Could anyone please help me find out what's wrong with that? Thanks for help and sorry for my poor English.

like image 946
Theo Avatar asked Dec 15 '22 16:12

Theo


1 Answers

Problem 1

A published property in a helper does not show up in the Object Inspector.

That's right. Properties declared in helpers will never appear in the Object Inspector.

Problem 2

Virtual methods have no effect in class helpers.

Again, that's by design. Although the compiler allows you to define virtual methods in helpers, it doesn't have any effect. In order for TCustomEditHelper.OrganizerChanged to fire, TCustomEditHelper needs to be the active helper for the target object.

Problem 3

The Change method for my intercepted TEdit class does not fire.

That's because you are not instantiating the intercepted TEdit. If you instantiate the right class, the interceptor class that you define, that method will fire.

like image 127
David Heffernan Avatar answered Dec 28 '22 03:12

David Heffernan