{"id":353,"date":"2019-10-30T16:11:38","date_gmt":"2019-10-30T22:11:38","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=353"},"modified":"2021-09-06T11:37:47","modified_gmt":"2021-09-06T17:37:47","slug":"spot-the-bug","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/10\/30\/spot-the-bug\/","title":{"rendered":"Spot the bug"},"content":{"rendered":"\n<p>You might run into this one from time to time. I know it has bitten a LOT of people and it&#8217;s not intuitive at all.<\/p>\n\n\n\n<p>In a new desktop app add a new Class. The default name of <em>Class1<\/em> will work fine. And create a new subclass of this. Again the default of <em>CustomClass1<\/em> will work fine.<\/p>\n\n\n\n<p>Then in the default window add the open event handler. In there put these few lines of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim c As Class1\nc = methodThatCreatesSubclassInstance\nfoo(c)<\/code><\/pre>\n\n\n\n<p>Then add two methods to Window1<\/p>\n\n\n\n<p>The first should be defined as<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Private Function methodThatCreatesSubclassInstance() as Class1\n  Return New CustomClass1\nEnd Function\n<\/code><\/pre>\n\n\n\n<p>And then we need two versions of Foo<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Private Sub foo(c as Class1)\n  Break\nEnd Sub\n\nPrivate Sub foo(c as CustomClass1)\n  Break  \nEnd Sub\n<\/code><\/pre>\n\n\n\n<p>Thats it. If you run this you might think that because <em>methodThatCreatesSubclassInstance<\/em> returns a <em>CustomClass1<\/em>, which you can see in the debugger at runtime, that the sub that takes a <em>CustomClass1<\/em> would be the one that is called.<\/p>\n\n\n\n<p>And you might try various tricks to make that happen like changing the return type of <em>methodThatCreatesSubclassInstance<\/em> to be <em>CustomClass1<\/em>.<\/p>\n\n\n\n<p>Short of explicitly casting, which you really might want to avoid, you&#8217;ll find that the version of <em>Foo<\/em> that takes a <em>Class1<\/em> parameter is the one that gets called. <\/p>\n\n\n\n<p>And this is something I&#8217;ve long considered a bug. But various compiler developers have disagreed \ud83d\ude41<\/p>\n\n\n\n<p>The important thing here is not the runtime type that the variable <em>c<\/em> holds. At runtime you can inspect it and see that it has exactly what we expected &#8211; an instance of <em>CustomClass1<\/em>.<\/p>\n\n\n\n<p>What IS important is the <strong>declared type <\/strong>of c &#8211; a <em>Class1<\/em>. And the compiler decides, at compile time, that the version of <em>Foo<\/em> that should be used is the one that has a <em>Class1<\/em> parameter.<\/p>\n\n\n\n<p>Careful out there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might run into this one from time to time. I know it has bitten a LOT of people and it&#8217;s not intuitive at all. In a new desktop app add a new Class. The default name of Class1 will work fine. And create a new subclass of this. Again the default of CustomClass1 will &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/10\/30\/spot-the-bug\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Spot the bug&#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,3],"class_list":["post-353","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-tips","tag-xojo"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/353","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=353"}],"version-history":[{"count":3,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/353\/revisions"}],"predecessor-version":[{"id":1246,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/353\/revisions\/1246"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}