{"id":665,"date":"2020-03-30T10:12:00","date_gmt":"2020-03-30T16:12:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=665"},"modified":"2020-03-28T10:31:35","modified_gmt":"2020-03-28T16:31:35","slug":"operator_convert-2","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/03\/30\/operator_convert-2\/","title":{"rendered":"Operator_convert"},"content":{"rendered":"\n<p>In a recent discussion on the forums someone said that &#8220;operator_convert acts like a constructor&#8221;.<\/p>\n\n\n\n<p>It&#8217;s true, except when its not.<\/p>\n\n\n\n<p>For instance, if you create a class that has an &#8220;operator_convert from&#8221; then you may be surprised at the behaviour.<\/p>\n\n\n\n<p>So first, whats an &#8220;operator_convert from&#8221; ?<\/p>\n\n\n\n<p>A class can have two forms of operator_convert. One takes a parameter. And one doesnt. For instance if I have a class, Box, that can be created from a string there are several ways to do this.<\/p>\n\n\n\n<p>You might write one that is a constructor that takes a string that holds the top, left, width and height<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub Constructor(constructFromString as String)<\/code><\/pre>\n\n\n\n<p>But then your code has to do something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim b as new Box( \"0,0,10,10\" )<\/code><\/pre>\n\n\n\n<p>But this isnt always quite as convenient as we&#8217;d like. One other option is to provide the &#8220;operator_convert from&#8221; form of Operator_convert. That would be a nethod you add that looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub operator_convert(constructFromString as String)<\/code><\/pre>\n\n\n\n<p>And this form lets you write code that looks like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim b as Box =  \"0,0,10,10\" <\/code><\/pre>\n\n\n\n<p>This appears a little nicer. So yes, in this case operator_convert from acts a LOT like a constructor. <\/p>\n\n\n\n<p>However, if your constructor sets some internal state that the instance requires note that Operator_Convert does NOT call the constructor at all.<\/p>\n\n\n\n<p>You can see this in action if, in th case of the Box class above, we put a break statement in the Constructor and then use the operator_convert from form. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Box\n   Sub Constructor(constructFromString as string)\n     break\n   End Sub\n   Sub Constructor()\n     break\n   End Sub\n   Sub Operator_convert(constructFromString as string)\n   End Sub\nEnd Class\n\ndim b as Box = \"10,10,50,50\"\n<\/code><\/pre>\n\n\n\n<p>You&#8217;ll find that you DO get a new instance BUT neither constructor is called. so IF you rely on the fact that your constructor HAS configured some properties in a particular way you might need to manually call the constructor yourself in the Operator_Convert from Form.<\/p>\n\n\n\n<p>You can overload operator_convert from and convert from as many types as you want for your. needs<\/p>\n\n\n\n<p>The second form of operator_convert is the &#8220;operator_convert to&#8221; form. This one is code you can use to take a custom class and return a different representation of the class..You might want to return it as a string, a double, or just about any other form you might need.<\/p>\n\n\n\n<p>In our box example above the operator_convert to form might return a string. It might look like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Sub operator_convert() as String<\/code><\/pre>\n\n\n\n<p>Note this form never takes a parameter. And you can have as many as you want as long as they do not cause any ambiguity. If you add one that converts to integer then don&#8217;t try and add one for int32 and int64 since integer is either an int32 or and int64 as you will get compilation errors.<\/p>\n\n\n\n<p>Both forms of operator_convert are VERY handy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent discussion on the forums someone said that &#8220;operator_convert acts like a constructor&#8221;. It&#8217;s true, except when its not. For instance, if you create a class that has an &#8220;operator_convert from&#8221; then you may be surprised at the behaviour. So first, whats an &#8220;operator_convert from&#8221; ? A class can have two forms of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/03\/30\/operator_convert-2\/\" 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_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-665","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/665","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=665"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions"}],"predecessor-version":[{"id":666,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/665\/revisions\/666"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}