{"id":4940,"date":"2014-03-30T17:09:28","date_gmt":"2014-03-30T17:09:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/passing-methods-as-parameters-on-a-deserialized-form-with-no-classtype-collection-of-common-programming-errors\/"},"modified":"2014-03-30T17:09:28","modified_gmt":"2014-03-30T17:09:28","slug":"passing-methods-as-parameters-on-a-deserialized-form-with-no-classtype-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/passing-methods-as-parameters-on-a-deserialized-form-with-no-classtype-collection-of-common-programming-errors\/","title":{"rendered":"Passing methods as parameters on a deserialized form with no ClassType-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m effectively trying to deserialize a form.<\/p>\n<p>One of the objects on the serialized form has a method which takes a series of events as parameters.<\/p>\n<p>Now since I don&#8217;t have the class type of the object when I&#8217;m deserializing, I have a method on the object doing the deserialization called AddMethod which is declared like this:<\/p>\n<pre><code>procedure TMyDeserializer.AddMethod(ControlName, EventName: String;\n  MethodAddr: Pointer);\nvar\n    TargetControl : TControl;\n    Method : TMethod;\nbegin\n    if Not Assigned(TempForm) then\n        Exit;\n    if TempForm.Name = ControlName then\n        TargetControl := TempForm\n    else\n        TargetControl := TempForm.FindChildControl(ControlName);\n\n    if Assigned(TargetControl) then\n    begin\n        Method.Code := MethodAddr;\n        Method.Data := TargetControl;\n        SetMethodProp(TargetControl, EventName, Method);\n    end;\nend;\n<\/code><\/pre>\n<p>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.<\/p>\n<pre><code>SetUpEvents(EventHandler1:TNotifyEvent;EventHandler2:TNotifyEvent);\n<\/code><\/pre>\n<p>Where EventHandler1 and EventHandler2 are defined somewhere in code as<\/p>\n<pre><code>Procedure EventHandler1(Sender:TNotifyEvent);  \nbegin\n    \/\/ Do something\nend;\n<\/code><\/pre>\n<p>These are not methods but stand alone subroutines.<\/p>\n<p>When I&#8217;m assigning these to objects the subroutine doesn&#8217;t need to be part of an object as the AddMethod procedure handles it with a call like<\/p>\n<pre><code>MyDeserializerInstance.AddMethod('Button1','OnClick',@EventHandler1);\n<\/code><\/pre>\n<p>This works for standard event handlers, such as Button1.OnClick but not if I want to do<\/p>\n<pre><code>Procedure SetUpButton1Click(Method: TNotifyEvent)\nbegin\n    TButton(MyDeserializerInstance.TempForm.FindChildControl('Button1')).OnClick = Method;\nend;\n<\/code><\/pre>\n<p>The problem is I can&#8217;t pass the subroutine as a method to the example Set Up Procedure.<\/p>\n<p>The form being created isn&#8217;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.<\/p>\n<p>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&#8217;t do that how do I pass the subroutines in code as parameters in another method?<\/p>\n<p>So far I&#8217;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.<\/p>\n<p>Delphi version is 2007<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8217;t have the class type of the object when I&#8217;m deserializing, I have a method on the object doing the deserialization called AddMethod which is declared [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4940","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4940","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4940"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4940\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4940"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4940"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4940"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}