{"id":40,"date":"2019-05-31T09:30:27","date_gmt":"2019-05-31T09:30:27","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=40"},"modified":"2019-06-21T01:39:03","modified_gmt":"2019-06-21T01:39:03","slug":"making-a-constructor-sometimes-illegal-to-call","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2019\/05\/31\/making-a-constructor-sometimes-illegal-to-call\/","title":{"rendered":"Making a constructor sometimes illegal to call"},"content":{"rendered":"\n<p>Suppose you write a control subclass that you want people to be able to add to a window layout OR be able to create on the fly. But you want to make sure that when an instance is constructed in code it MUST have a parameterized constructor. However, in order to put an instance on a layout by drag and drop it has to have a no parameter constructor. These two design goals seem very much at odds.<\/p>\n\n\n\n<p>So how do you make this happen ?<\/p>\n\n\n\n<p>Such a control might be like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class myControl\n  Inherits Canvas\n\n  Sub Constructor()\n  End Sub\n\n  Sub Constructor(name as string)\n     self.mName = name\n  End Sub\n\n  Computed Property MyName as string\n    Function Getter() as string\n       return mName\n    End Function\n    Sub Getter(value as string)\n       mName = value\n    End Sub\n   \n  protected mName as string\nend class<\/code><\/pre>\n\n\n\n<p>We&#8217;ll assume that MyName has been exposed as part of the Inspector Behaviour.<\/p>\n\n\n\n<p>But the class definition above would definitely allow you to write code that called either constructor. How to make it so the no parameter constructor can only be used by instances on a layout that, as part of initializing the layout, basically does the following <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim c as new myControl\nc.MyName = \"whatever value the user set in the inspector\"<\/code><\/pre>\n\n\n\n<p>But there is a trick you can use to know how you have been called. An exception exposes the call stack to you and you can examine that to know if there is a window being initialized that has resulted in your no parameter constructor being called. And so you can make it so a window being initialized can use the no parameter version. In fact to make it so your control can be placed on a layout in the IDE designer it MUST have a no parameter constructor if it has any constructors.<\/p>\n\n\n\n<p>If we make that no parameter constructor read as follows we can make this raise an exception when used improperly outside of a window being constructed. Sorry can&#8217;t make the compiler reject it (THAT would be awesome)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Try\n  Raise New NilObjectException\n  \nCatch noe As NilObjectException\n  Dim stack() As String = noe.Stack\n  \n  \/\/ IF this is an instance created on a layout we should find a \n  \/\/ Window.__Init%%o&lt;Window> in the stack above the call to this Constructor\n  For i As Integer = 0 To stack.Ubound\n    If stack(i) = \"Window.__Init%%o&lt;Window>\" Then\n      Return\n    End If\n  Next\n  \n  \/\/ and if we do not then raise an UnsupportedOperation error\n  Raise New UnsupportedOperationException\nEnd Try\n<\/code><\/pre>\n\n\n\n<p>A window, when being created, calls __Init and so we can tell if we were called by a window being created or not by walking up the call stack. If there is no Window.__Init in the stack then we definitely were not and so can raise an exception to let the coder know that this usage, without a parameter, is only for Layouts and nothing else.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you write a control subclass that you want people to be able to add to a window layout OR be able to create on the fly. But you want to make sure that when an instance is constructed in code it MUST have a parameterized constructor. However, in order to put an instance on &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2019\/05\/31\/making-a-constructor-sometimes-illegal-to-call\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Making a constructor sometimes illegal to call&#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-40","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\/40","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=40"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"predecessor-version":[{"id":150,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/40\/revisions\/150"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}