{"id":674,"date":"2020-04-10T08:10:00","date_gmt":"2020-04-10T14:10:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=674"},"modified":"2020-03-29T08:30:01","modified_gmt":"2020-03-29T14:30:01","slug":"issues-with-creation-the-answer","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/04\/10\/issues-with-creation-the-answer\/","title":{"rendered":"Issues with creation &#8211; the answer"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Yeah. 42. I know that answer \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So far what we have is<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Module PictureMimicry\n\n    Class MyPicture\n       Function MyGraphics() as MyGraphics\n       End Function\n    End Class\n\n    Class MyGraphics\n    End Class\nEnd Module\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">And now, for the answer, on how to make it so we can make the compiler catch the attempts to create a new MyGraphics.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First we ABSOLUTELY put a protected constructor on MyGraphics. That will prevent EVERY attempt to create a new MyGraphics with the NEW operator. But now how does MyPicture create one to return it ?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It doesnt \ud83d\ude42<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Remember we can set the SCOPE of items in a way that they can ONLY be used by code in the module. And since Xojo IS an OO language and support inheritance and polymorphism we can use that to our advantage.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In such a language every subclass of a class IS also its parent class type. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What we&#8217;re going to do is add a private subclass whose sole purpose is to allow us to create new instances. And we can return that subclass and the code outside the module will ONLY be able to use it as the parent class type because the subclass type is not know outside he module.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The end result is this set up<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Module PictureMimicry\n\n    Class MyPicture\n       Function MyGraphics() as MyGraphics\n         return new MyGraphicsSubclass\n       End Function\n    End Class\n\n    Class MyGraphics\n      Protected Sub Constructor()\n      End Sub\n    End Class\n\n    Private Class MyGraphicsSubclass\n        Inherits MyGraphics\n\n      Public Sub Constructor()\n      End Sub\n\n    End Class\n\nEnd Module<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and now in the App Open event from the previous post<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim p as MyPicture\ndim g as MyGraphics\n\np = new MyPicture\ng = new MyGraphics<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The compiler will complain about the last line not being permitted. Exactly as we wanted.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And this code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim p As MyPicture\nDim g As MyGraphics\n\np = New MyPicture\ng = p.MyGraphics<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Works exactly as we intended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now freely add methods to MyGraphics and MyPicture and know that none of your own code can accidentally create a MyGraphics in a way that you did not intend to permit.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Code in a way the compiler can help you write code the way you intended it to work. Not just by convention or habit. But by design.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Code for your own safety and sanity. Get the compiler to help you when it can.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yeah. 42. I know that answer \ud83d\ude42 So far what we have is And now, for the answer, on how to make it so we can make the compiler catch the attempts to create a new MyGraphics. First we ABSOLUTELY put a protected constructor on MyGraphics. That will prevent EVERY attempt to create a new &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/04\/10\/issues-with-creation-the-answer\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Issues with creation &#8211; the answer&#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":[],"class_list":["post-674","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/674","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=674"}],"version-history":[{"count":2,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions"}],"predecessor-version":[{"id":676,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/674\/revisions\/676"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}