Helper class and class interceptor in Delphi XE2

I have a piece of code as shown below:

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; 

:

 {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 could not solve on my own.

Task 1: The published property of a property does not appear in the object inspector. I think I made a mistake and forgot something.

problem 2: the overridden method from the helper class, as shown above, did not work (did not work).

problem 3: the overridden method from the interceptor class, as shown above, did not work (did not work).

Can someone help me find out what happened to this? Thanks for the help and sorry for my poor English.

+4
source share
2 answers

Problem 1

The published property in the helper does not appear in the object inspector.

It is right. Properties declared in helpers will never be displayed in the object inspector.

Problem 2

Virtual methods do not affect class helpers.

Again that by design. Although the compiler allows you to define virtual methods in helpers, this has no effect. To run TCustomEditHelper.OrganizerChanged TCustomEditHelper must be the active helper for the target.

Problem 3

The Change method for my intercepted TEdit class TEdit not work.

This is because you are not creating an instance of the intercepted TEdit . If you create the correct class, the interceptor class that you define, this method will fire.

+8
source

The helper class can be considered as a regular non-object oriented function that takes an object as its first parameter. Helpers allow you to hide this and code as if you were calling methods. If you see them as simple functions, you can understand why they do not appear in the object inspector or anywhere where access to internal objects is used (for example, virtual method tables, dispatch methods and published properties), because there are no internal changes to the object .

+3
source

All Articles