{"id":447,"date":"2019-12-17T11:33:00","date_gmt":"2019-12-17T18:33:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=447"},"modified":"2019-12-03T18:11:14","modified_gmt":"2019-12-04T01:11:14","slug":"operator_compare","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/12\/17\/operator_compare\/","title":{"rendered":"Operator_compare"},"content":{"rendered":"\n<p>Ever wanted to define a class, maybe a vector class, and make it possible to write code like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim v1 as new Vector(1,1)\ndim v2 as new Vector(2,2)\n\nif v1 > v2 then\n  \/\/ do something when v1 > v2\nelseif v1 &lt; v2 then\n  \/\/ do something when v1 &lt; v2\nelse\n  \/\/ do something when v1 = v2\nend if<\/code><\/pre>\n\n\n\n<p>Perhaps you have some other kind of class you&#8217;d like to define that has some kind of custom mechanism to compare itself to other instances &#8211; or even other data types.<\/p>\n\n\n\n<p>If so you need to know how to implement operator_compare<\/p>\n\n\n\n<p>First off operator_compare can ONLY be implemented in a Class. You cannot extend existing classes and add it to them. <\/p>\n\n\n\n<p>It&#8217;s a method, or several methods, you add to a class that will be called whenever you use the comparison operators =, &lt;, >, &lt;=, >= and &lt;> are used. Note that this has implications for your code if you are using = to check is one instance is the same instance as another. In that case you should probably use IS rather than =.<\/p>\n\n\n\n<p>Beyond that operator_compare is pretty straight forward.<\/p>\n\n\n\n<p>You define the method with that name, and the parameter is whatever type you wish to compare your instance, the &#8220;self&#8221; instance, to. So you can have many as each signature would be different. The return value is and integer that is<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>&lt; 0 means SELF is &#8220;less than&#8221; the passed parameter<\/li><li>= 0 means SELF is &#8220;equal to&#8221; the passed parameter<\/li><li>> 0 means SELF is &#8220;greater than&#8221; the passed parameter<\/li><\/ul>\n\n\n\n<p>and you get to define what <em>less than<\/em>, <em>equal to<\/em>, and <em>greater than<\/em> mean.<\/p>\n\n\n\n<p>So suppose our Vector class was defined as follows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Vector\n   protected x as double\n   protected y as double\n\n   Public Sub Constructor(x as double, y as double)\n      Self.x = x\n      Self.y = y\n   End Sub\n\n   Function Operator_compare(other as vector) as Integer\n      Dim a, b As Integer\n      a = Self.x ^ 2 + Self.y ^ 2\n      b = other.x ^ 2 + other.y ^ 2\n      If a > b Then Return 1\n      If a = b Then Return 0\n      If a &lt; b Then Return -1\n    End Function\nEnd Class<\/code><\/pre>\n\n\n\n<p>And now our original code from way back would work<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim v1 as new Vector(1,1)\ndim v2 as new Vector(2,2)\n\nif v1 > v2 then\n  \/\/ do something when v1 > v2\nelseif v1 &lt; v2 then\n  \/\/ do something when v1 &lt; v2\nelse\n  \/\/ do something when v1 = v2\nend if<\/code><\/pre>\n\n\n\n<p>Now you could make Vectors compare themselves to Strings if you really wanted. I&#8217;m not sure what exactly that might mean &#8211; but you could. You might try implementing this as <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Public Function Operator_compare1(other As string) as Integer\n  Dim myRepresentation As String = \"(\" + Str(x,\"-######.00\") + \",\" + Str(y,\"-######.00\") + \")\"\n  \n  If myRepresentation = other Then Return 0\n  \n  return -1\nEnd Function<\/code><\/pre>\n\n\n\n<p>Again I&#8217;m not sure what this might truly mean but we&#8217;ll indicate that unless they are the exact same string the &#8220;self&#8221; is &lt; the string value. Suppose you try to test this with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim v1 As New Vector(1,1)\n\nIf v1 = \"(foo)\" Then\n  Break\nElse\n  Break\nEnd If<\/code><\/pre>\n\n\n\n<p>And now you Vectors can be compared to Strings. With extra overloads of Operator_compare you can make your classes compare themselves to all sorts of your other classes &amp; types.<\/p>\n\n\n\n<p>Enjoy !<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wanted to define a class, maybe a vector class, and make it possible to write code like Perhaps you have some other kind of class you&#8217;d like to define that has some kind of custom mechanism to compare itself to other instances &#8211; or even other data types. If so you need to know &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/12\/17\/operator_compare\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Operator_compare&#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-447","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\/447","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=447"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/447\/revisions"}],"predecessor-version":[{"id":469,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/447\/revisions\/469"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=447"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=447"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=447"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}