{"id":460,"date":"2019-12-05T16:25:00","date_gmt":"2019-12-05T23:25:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=460"},"modified":"2019-12-02T12:25:50","modified_gmt":"2019-12-02T19:25:50","slug":"operator_convert","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/12\/05\/operator_convert\/","title":{"rendered":"Operator_Convert"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Operator_Convert is a very handy method to implement on a lot of classes. It not only lets you create a mechanism to make your classes convert themselves into other types but it also lets you create new instances FROM other data types.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So let&#8217;s start with the simple forms of operator_convert &#8211; the &#8220;convert TO&#8221; forms.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose you have some class defined called &#8220;myClass&#8221; <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class myClass\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and you want to make it easy to have this class write log messages to the DebugLog. You most certainly could write a &#8220;ToString&#8221; method as an explicit way to convert this class to a String<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class myClass\n   Function ToString() as string\n   End Function\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"> and then in your code you&#8217;d use it like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim instance as new MyClass\n\n\/\/ lots of intervening code\n\nsystem.debuglog instance.ToString<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And maybe this isn&#8217;t so bad. It IS explicit which is most times a good thing. Your code will have a certain clarity to it because of this style.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">But there is another way to do much the same thing that is also effective &#8211; despite it being slightly hidden and magical like a variants ability to automatically convert itself to other data types by assignment. And this &#8220;magic&#8221; is operator_convert<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a class like our sample<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class myClass\n   Function Operator_Convert() as string\n   End Function\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now your usage could look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim instance as new MyClass\n\n\/\/ lots of intervening code\n\nsystem.debuglog instance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Since System.Debuglog takes a string as a parameter the compiler figures out that our class has a suitable Operator_convert method and will call that for us. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can define many operator_convert functions that convert TO other datatypes and as long as they dont cause any ambiguity you will get the results you expect. You may get an ambiguous situation of you define a Convert To operator_convert for Integer, Int32 and\/or Int64 since Integer is simply an alias to one of those types depending on whether you are building for 32 or 64 bit. There can be others but this is the most common.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is another form for operator_convert &#8211; the &#8220;convert FROM&#8221; form.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this case operator_convert behaves in many ways like a constructor called with a parameter. However one thing to remember is that Operator_convert is NOT a constructor! Its an initializer at best. If your class depends on constructors being called make sure when you use the convert FROM form you call your constructor !<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For our sample class suppose we want to initialize it from a string.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class myClass\n   Function Operator_Convert() as string\n   End Function\n\n   Sub Operator_Convert(initializeWith as string)\n      \/\/ if we rely on the constructor having been called \n      \/\/ then call the Constructor here !\n       \n      \/\/ now set our class up from whatever data is in the\n      \/\/ parameter passed\n   End Sub\n\nEnd Class<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and we could use it like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim instance as MyClass = \"123\"\n\n\/\/ lots of intervening code\n\nsystem.debuglog instance<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Something to note is that these conversions can appear to be hidden away from a casual reader so use them with care and consideration. Often explicit code, like the ToString example above aids a reader in understanding the code because its right there in front of them that you are asking the class to give you a string representation for some purpose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep that in mind and use these with that in mind.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Enjoy !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Operator_Convert is a very handy method to implement on a lot of classes. It not only lets you create a mechanism to make your classes convert themselves into other types but it also lets you create new instances FROM other data types. So let&#8217;s start with the simple forms of operator_convert &#8211; the &#8220;convert TO&#8221; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/12\/05\/operator_convert\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Operator_Convert&#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_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[6,3],"class_list":["post-460","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-tips","tag-xojo"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/460","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=460"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions"}],"predecessor-version":[{"id":465,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/460\/revisions\/465"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}