Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I wrote a anonymous event creator. I would like to ask if anyone can tell me if I did it the "right" or "best" way, or how I could make it better.

Here is what I did:

Declaration:

  TAnonymousEvents<TTEventPointer,TTEventReference>=class
  public
    class function Create(AEvent:TTEventReference):TTEventPointer;
  end;

Implementation:

class function TAnonymousEvents<TTEventPointer,TTEventReference>.Create(AEvent: TTEventReference): TTEventPointer;
type
  TVtable = array[0..3] of Pointer;
  PVtable = ^TVtable;
  PPVtable = ^PVtable;
begin
  TMethod(result).Code := PPVtable(AEvent)^^[3];
  TMethod(result).Data := Pointer(AEvent);
end;

Usage:

.....

type TNotifyEventReference = reference to procedure (Sender: TObject);

.....

procedure TForm1.FormCreate(Sender: TObject);
begin
  Button1.OnClick := TAnonymousEvents<TNotifyEvent,TNotifyEventReference>.Create(
    procedure (Sender:TObject)
    begin
      ShowMessage('it works!');
    end
  );
end;
share|improve this question
    
I don't see the declaration of TTEventPointer and TTEventReference –  hubalu Mar 18 at 11:54
    
hi @hubalu, they are generic parameters. –  linluk Mar 19 at 13:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.