{"id":406,"date":"2019-11-07T11:02:12","date_gmt":"2019-11-07T18:02:12","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=406"},"modified":"2019-11-19T09:49:34","modified_gmt":"2019-11-19T16:49:34","slug":"extension-methods","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/11\/07\/extension-methods\/","title":{"rendered":"Extension methods"},"content":{"rendered":"\n<p>Sometimes you run into a situation where you want to add functionality to an existing data type. It might be a string, integer, array or perhaps one of the built in classes that you want to add new functionality to.<\/p>\n\n\n\n<p>Exactly how do you do this ?<\/p>\n\n\n\n<p>You can&#8217;t just edit the framework and add new methods to the <em>String<\/em> type.<\/p>\n\n\n\n<p>And that is probably a good thing. Imagine if every user could edit <em>String<\/em> and add methods to it. There might be a huge plethora of incompatible implementations of <em>String<\/em> very rapidly.<\/p>\n\n\n\n<p>Since you cannot edit <em>String<\/em> what can you do ?<\/p>\n\n\n\n<p>Extension methods!<\/p>\n\n\n\n<p>You CAN create a method type that is almost indistinguishable from a method in the base type itself. First I would suggest creating a Module named with a meaningful name. For a Module to add new method to the <em>String<\/em> data type I would name the module <em>StringExtensions<\/em> so its very clear what this module contains.<\/p>\n\n\n\n<p>The in this module you can add GLOBAL methods. And each method in the module should have a signature that uses the <em>extends<\/em> keyword.<\/p>\n\n\n\n<p>I&#8217;ll demonstrate. Suppose we wanted to add a new method to String that checks if the first letter of the string is a digit. We&#8217;ll name that method <em>IsDigit<\/em>. <\/p>\n\n\n\n<p>For create a new desktop project.<\/p>\n\n\n\n<p>Add a Module to the project called <em>StringExtensions<\/em>.<\/p>\n\n\n\n<p>Then add a method to the <em>StringExtensions<\/em> module and name the method <em>IsDigit<\/em>.<\/p>\n\n\n\n<p>The parameters for this method should be<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extends aString as String<\/code><\/pre>\n\n\n\n<p>Note the use of <em>EXTENDS<\/em>. This is the hint to the compiler that this method should be treated as if it were one of the methods in the framework for the <em>String<\/em> datatype.<\/p>\n\n\n\n<p>The return type for this method should be <em>Boolean<\/em>.<\/p>\n\n\n\n<p>The code in the method should be<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>If aString.Trim = \"\" Then \n  Return False\nEnd If\n\nIf InStr( \"0123456789\", aString.Left(1) ) > 0 Then\n  Return True\nEnd If\n\nreturn false\n\n<\/code><\/pre>\n\n\n\n<p>The finished method should appear like<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1234\" height=\"619\" src=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-04-at-8.44.29-AM.png?fit=525%2C264&amp;ssl=1\" alt=\"\" class=\"wp-image-415\" srcset=\"https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-04-at-8.44.29-AM.png?w=1234&amp;ssl=1 1234w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-04-at-8.44.29-AM.png?resize=300%2C150&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-04-at-8.44.29-AM.png?resize=768%2C385&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.great-white-software.com\/blog\/wp-content\/uploads\/2019\/11\/Screen-Shot-2019-11-04-at-8.44.29-AM.png?resize=1024%2C514&amp;ssl=1 1024w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>Then in the Open, or in 2019r2 the Opening event, you can test the new extension method with code like <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nDim s As String\n\ns = \"abc\"\nIf s.IsDigit Then\n  Break\nEnd If\n\ns = \"\"\nIf s.IsDigit Then\n  Break\nEnd If\n\ns = \"1abc\"\nIf s.IsDigit Then\n  Break\nEnd If\n<\/code><\/pre>\n\n\n\n<p>The first two cases should return false. And the last one should return true.<\/p>\n\n\n\n<p>The only thing to be careful about with extension methods is :<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>you cant name the Module the same as the datatype. The IDE will generally prevent you from doing this.<\/li><li>you have to not create a new extension that may conflict with one that is in the framework.<\/li><li>you have to be careful not to create one that will cause the compiler to decide there is an ambiguous use case where it cant decide which one to call. This can happen when you use optional parameters.<\/li><\/ul>\n\n\n\n<p>But other than that you can extend most datatypes, even ones in the framework, relatively easily.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you run into a situation where you want to add functionality to an existing data type. It might be a string, integer, array or perhaps one of the built in classes that you want to add new functionality to. Exactly how do you do this ? You can&#8217;t just edit the framework and add &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/11\/07\/extension-methods\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Extension methods&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[6],"class_list":["post-406","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-tips"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/406","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=406"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/406\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}