{"id":1042,"date":"2010-06-02T09:29:49","date_gmt":"2010-06-02T09:29:49","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1042"},"modified":"2010-06-02T09:29:49","modified_gmt":"2010-06-02T09:29:49","slug":"delegates-settings-function-on-the-fly","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/06\/02\/delegates-settings-function-on-the-fly\/","title":{"rendered":"delegates &#8211; settings function on the fly"},"content":{"rendered":"<p>Delegates allow a virtual method\/function (static method) to be called via a link (which is the delegated variable).  I have done a post before about <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/18\/delegates-csharp\/\">delegates<\/a> but kinder think and also have been asked how do you link one to a class and also change the delegated function due to a user input, so here goes.<\/p>\n<p>I have started with setting up what type of method that I want to be associated with the delegated variable, this will be a mathematical function of either addition or subtraction with using integer values, so we need to return a integer value and also pass in two integer parameters, so a new delegated virtual method would be<\/p>\n<pre lang=\"csharp\">\r\npublic delegate int mathsOp(int value1, int value2);\r\n<\/pre>\n<p>and here would be a example of a method that the delegate is able to link to, because it takes 2 integer parameters and returns a integer value<\/p>\n<pre lang=\"csharp\">\r\npublic int add(int value1, int value2)\r\n{\r\n\treturn value1 + value2;\r\n}<\/pre>\n<p>so we now have the delegate declaration and also a method to be able to point to, so we now need to setup the variables, one for the actual functions (MathClass that is holding the subtraction and addition methods) and also the delegated variable theMathOp that is a delegated type.<\/p>\n<pre lang=\"csharp\">\r\nMathClass mathFunc = new MathClass();\r\n\r\nmathsOp theMathOp;\r\n<\/pre>\n<p>to actually set the method up on the fly, you just need to tell it where you want the delegated type to point to<\/p>\n<pre lang=\"csharp\">\r\ntheMathOp = new mathsOp(mathFunc.add); \r\n<\/pre>\n<p>and all is needed to call the delegated type variable, well you just call it like any other method<\/p>\n<pre lang=\"csharp\">\r\ntheMathOp(inputValue1, inputValue2);\r\n<\/pre>\n<p>that is about it, here is the code in full that will take in some values from the user and also the user is able to choose between addition and subtraction methods<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\n\r\nnamespace newDelegates\r\n{\r\n\t\/\/ a maths holding delegated function\r\n\tpublic delegate int mathsOp(int value1, int value2);\r\n\t\r\n\tclass MathClass\r\n\t{\r\n\t\t\/\/ the functions to call within the class\r\n\t\tpublic int add(int value1, int value2)\r\n\t\t{\r\n\t\t\treturn value1 + value2;\r\n\t\t}\r\n\t\t\r\n\t\tpublic int sub(int value1, int value2)\r\n\t\t{\r\n\t\t\treturn value1 - value2;\r\n\t\t}\r\n\t\t\r\n\t}\r\n\t\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main (string[] args)\r\n\t\t{\r\n\t\t\t\r\n\t\t\t\/\/ setup the maths class which has the functions inside to call\r\n\t\t\tMathClass mathFunc = new MathClass();\r\n\t\t\t\r\n\t\t\tmathsOp theMathOp;\r\n\t\t\t\r\n\t\t\t\/\/ there is no error checking in the inputs!!\r\n\t\t\t\r\n\t\t\tConsole.Write(\"Please enter value 1 : \");\r\n\t\t\tint inputValue1 = Convert.ToInt16(Console.ReadLine());\r\n\t\t\tConsole.Write(\"Please enter value 2 : \");\r\n\t\t\tint inputValue2 = Convert.ToInt16(Console.ReadLine());\r\n\t\t\t\r\n\t\t\tConsole.WriteLine(\"Please enter maths function :\");\r\n\t\t\tConsole.WriteLine(\"1 : add\");\r\n\t\t\tConsole.WriteLine(\"2 : sub\");\r\n\t\t\tint mathsInputFun = Convert.ToInt16(Console.ReadLine());\r\n\t\t\t\/\/ setup the virtual function to which ever the user wants\r\n\t\t\tswitch (mathsInputFun)\r\n\t\t\t{\r\n\t\t\t\tcase 1 : \r\n\t\t\t\t\ttheMathOp = new mathsOp(mathFunc.add); \r\n\t\t\t\t\tbreak;\r\n\t\t\t\tcase 2 : \r\n\t\t\t\t\ttheMathOp = new mathsOp(mathFunc.sub); \r\n\t\t\t\t\tbreak;\r\n\t\t\t\tdefault :\r\n\t\t\t\t\tConsole.WriteLine(\"Settings to add\");\r\n\t\t\t\t\ttheMathOp = new mathsOp(mathFunc.add); \r\n\t\t\t\t\tbreak;\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tConsole.WriteLine(\"Value 1= \" + inputValue1 + (mathsInputFun == 1 ? \" + \" : \" - \") \r\n\t\t\t                  + \" value 2 = \" + inputValue2 + \" = \");\r\n\t\t\t\r\n\t\t\t\/\/ here we call the virtual function that was setup\r\n\t\t\tConsole.WriteLine(theMathOp(inputValue1, inputValue2));\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and below is the output of two runs of the program, one using addition and the other using subtraction<\/p>\n<pre lang=\"bash\">\r\nPlease enter value 1 : 3\r\nPlease enter value 2 : 2\r\nPlease enter maths function :\r\n1 : add\r\n2 : sub\r\n1\r\nValue 1= 3 +  value 2 = 2 = 5\r\n\r\nsecond run through\r\n\r\nPlease enter value 1 : 5\r\nPlease enter value 2 : 2\r\nPlease enter maths function :\r\n1 : add\r\n2 : sub\r\n2\r\nValue 1= 5 -  value 2 = 2 = 3\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Delegates allow a virtual method\/function (static method) to be called via a link (which is the delegated variable). I have done a post before about delegates but kinder think and also have been asked how do you link one to a class and also change the delegated function due to a user input, so here &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/06\/02\/delegates-settings-function-on-the-fly\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">delegates &#8211; settings function on the fly<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[106],"class_list":["post-1042","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-delegates"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1042","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1042"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1042\/revisions"}],"predecessor-version":[{"id":1045,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1042\/revisions\/1045"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}