Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 956137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:29:50+00:00 2026-05-16T00:29:50+00:00

I’m effectively trying to deserialize a form. One of the objects on the serialized

  • 0

I’m effectively trying to deserialize a form.

One of the objects on the serialized form has a method which takes a series of events as parameters.

Now since I don’t have the class type of the object when I’m deserializing, I have a method on the object doing the deserialization called AddMethod which is declared like this:

procedure TMyDeserializer.AddMethod(ControlName, EventName: String;
  MethodAddr: Pointer);
var
    TargetControl : TControl;
    Method : TMethod;
begin
    if Not Assigned(TempForm) then
        Exit;
    if TempForm.Name = ControlName then
        TargetControl := TempForm
    else
        TargetControl := TempForm.FindChildControl(ControlName);

    if Assigned(TargetControl) then
    begin
        Method.Code := MethodAddr;
        Method.Data := TargetControl;
        SetMethodProp(TargetControl, EventName, Method);
    end;
end;

So that I can poke subroutines into the various controls as I deserialize them, The problem is I need to add events as a list of parameters (not to a control). e.g.

SetUpEvents(EventHandler1:TNotifyEvent;EventHandler2:TNotifyEvent);

Where EventHandler1 and EventHandler2 are defined somewhere in code as

Procedure EventHandler1(Sender:TNotifyEvent);  
begin
    // Do something
end;

These are not methods but stand alone subroutines.

When I’m assigning these to objects the subroutine doesn’t need to be part of an object as the AddMethod procedure handles it with a call like

MyDeserializerInstance.AddMethod('Button1','OnClick',@EventHandler1);

This works for standard event handlers, such as Button1.OnClick but not if I want to do

Procedure SetUpButton1Click(Method: TNotifyEvent)
begin
    TButton(MyDeserializerInstance.TempForm.FindChildControl('Button1')).OnClick = Method;
end;

The problem is I can’t pass the subroutine as a method to the example Set Up Procedure.

The form being created isn’t declared in an interface and is entirely defined by the file it is read from as well as a few stand alone routines in code.

So I suppose the question is how do turn a subroutine into a method at run time (after creating the object it is supposed to be part of), and if I can’t do that how do I pass the subroutines in code as parameters in another method?

So far I’ve tried casting a TMethod as the correct event type and filling in the .Data as the TempForm. It called the correct method but it scrambled the parameters.

Delphi version is 2007

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:29:51+00:00Added an answer on May 16, 2026 at 12:29 am

    Non-static class methods have a hidden Self input parameter that is filled in when the method is called. That is what the TMethod.Data field corresponds to. In order to use a standalone procedure as a handler for an event that expects a class method, the procedure must have an extra parameter defined to represent the Self parameter so the value of TMethod.Data has somewhere to go, ie:

    procedure Button1ClickHandler(Self: Pointer; Sender: TObject);
    begin 
      // Do something 
    end; 
    
    MyDeserializerInstance.AddMethod('Button1', 'OnClick', @Button1ClickHandler);
    

    Your AddMethod() implementation is assigning the TargetControl as the TMethod.Data value, so the Self and Sender parameters above will end up pointing at the same object at runtime, but that is OK.

    Without the explicit Self parameter defined, that explains why your parameters are getting “scrambled” when the procedure called at runtime. The hidden Self value is being assigned to the Sender parameter, and the real Sender value is being ignored.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.