{"id":106,"date":"2010-03-17T16:15:53","date_gmt":"2010-03-17T16:15:53","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=106"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"creating-simple-callback-function","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/delegates\/creating-simple-callback-function","title":{"rendered":"Creating Simple Callback Function"},"content":{"rendered":"<p>Here&#8217;s step by step instructions of how to create a callback function which allows a function in one class to be called by some other class it is using (for instance, you may have a class you&#8217;ve created that provides communication functions and you want to pass received data back to the main class or your form whenever it gets received).<\/p>\n<pre><code>\r\n\/\/TO ADAPT FOR EACH USE, FIND AND REPLACE THE FOLLOWING:\r\n\/\/\tNAME\t\t\t\t\t\tCHANGE TO\r\n\/\/\tThisClassName\t\t\t\tName of the class that contains the function to be called.\r\n\/\/\tProcessNewEventFunction\t\tName of the function that will be called by the callback (in the class that contains the function to be called).\r\n\/\/\tProcessNewEventFunction_Def\tSame as the name used ProcessNewEventFunction but made different, e.g. by adding _def to the end.\r\n\/\/\tOtherClassName\t\t\t\tName of the class that will be calling back (the other class)\r\n\/\/\tOtherClassObject1\t\t\tName of the other class object in the class which will be called back\r\n\/\/\tSetMyCallbackFunction\t\t\tFunction to call in the other class to set the callback function it should call back\r\n\/\/\tOtherClassCallbackFunction1\t\tObject in the other class which stores the function to be called back.\r\n<\/code><\/pre>\n<h4>In Class That Contains Function To Be Called<\/h4>\n<h5>Create the function to be called:<\/h5>\n<pre><code>\r\n\tpublic: void ThisClassName::ProcessNewEventFunction (String ^SomeTextIWantToPassForExample)\r\n\t{\r\n\t}\r\n<\/code><\/pre>\n<h5>Call other class function that we are passing the callback delegate to:<\/h5>\n<pre><code>\r\n\t\/\/Set our callback function\r\n\tProcessNewEventFunction_Def ^del = gcnew ProcessNewEventFunction_Def(this, &amp;ThisClassName::ProcessNewEventFunction); \t\/\/Class::FunctionName\r\n\tOtherClassObject1-&gt;SetMyCallbackFunction(del);\r\n<\/code><\/pre>\n<h4>In Class That Needs To Call The Function<\/h4>\n<h5>Define delegate handler somewhere outside of the class (i.e. inside .h file namespace but before public ref class)<\/h5>\n<pre><code>\r\n\tpublic delegate void ProcessNewEventFunction_Def (String ^SomeTextIWantToPassForExample);\r\n<\/code><\/pre>\n<h5>Define object to store passed delegate<\/h5>\n<pre><code>\r\n\tprivate: ProcessNewEventFunction_Def ^OtherClassCallbackFunction1;\r\n<\/code><\/pre>\n<h5>Create function to receive the delegate and store it<\/h5>\n<pre><code>\r\n\tpublic: void OtherClassName::SetMyCallbackFunction(ProcessNewEventFunction_Def ^del)\r\n\t{\r\n\t\tOtherClassCallbackFunction1 = del;\r\n\t}\r\n<\/code><\/pre>\n<h5>To call the function<\/h5>\n<pre><code>\r\n\tif (OtherClassCallbackFunction1 != nullptr)\r\n\t\tOtherClassCallbackFunction1(\"12345\");\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s step by step instructions of how to create a callback function which allows a function in one class to be called by some other class it is using (for instance, you may have a class you&#8217;ve created that provides communication functions and you want to pass received data back to the main class or [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-106","post","type-post","status-publish","format-standard","hentry","category-delegates"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/106","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=106"}],"version-history":[{"count":12,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/106\/revisions"}],"predecessor-version":[{"id":1140,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/106\/revisions\/1140"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=106"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=106"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=106"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}