{"id":795,"date":"2011-09-15T13:11:17","date_gmt":"2011-09-15T01:11:17","guid":{"rendered":"https:\/\/www.deltics.co.nz\/blog\/?p=795"},"modified":"2011-09-15T13:11:17","modified_gmt":"2011-09-15T01:11:17","slug":"when-does-createform-not-create-a-form","status":"publish","type":"post","link":"https:\/\/www.deltics.co.nz\/blog\/posts\/795\/","title":{"rendered":"When Does &#8220;CreateForm&#8221; Not Create a Form&#8230; ?"},"content":{"rendered":"<span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span><p>Apparently, when your application is a <strong>FireMonkey<\/strong> application.<br \/>\n<!--more--><br \/>\nIf you have any applications which contain code similar to this:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  Application.CreateForm(TMyForm, MyForm);\r\n\r\n  MyForm.InitialProperty := SomeInitialValue;\r\n\r\n  Application.Run;\r\n<\/pre>\n<p>Then you will need to rethink how you initialise those forms in the <strong>FireMonkey<\/strong> framework because <strong>CreateForm()<\/strong> commits what I consider to be a cardinal sin (in the software development world at least):<\/p>\n<h2>It doesn&#8217;t do what it says it does!<\/h2>\n<p>In a VCL application, when you call <strong>CreateForm( class, ref )<\/strong> an instance of <strong>class<\/strong> is created and a reference to it placed in <strong>ref<\/strong>, which you can then use to initialise or otherwise reference the instantiated form.<\/p>\n<p>Try and do this in a <strong>FireMonkey<\/strong> application and it blows up in your face.<\/p>\n<p>In a <strong>FireMonkey<\/strong> application when you call <strong>CreateForm<\/strong> no form is created at all!<\/p>\n<p>Instead, the <strong>Application<\/strong> object simply takes a note of the class and the address of the reference variable provided in an internal array.  No instance is created and the reference is not assigned until you call <strong>Application.Run<\/strong> (or alternatively the new <strong>Application.RealCreateForms<\/strong>).<\/p>\n<p>This all has a really, <em>really<\/em> bad smell about it and frankly beggars belief.<\/p>\n<p>Why go to the trouble of preserving the appearance of the behaviour of one framework in another if you are not going to preserve the behaviour itself ?<\/p>\n<p>Or put more simply &#8211; if <strong>FireMonkey<\/strong> works differently then <em>for crying out loud make it <strong>look<\/strong> different<\/em> in those areas where those differences are <strong>important<\/strong>!<\/p>\n<p>In this case, <strong>CreateForm()<\/strong> should have been replaced by something which was clearer in conveying the intended and expected behaviour (or rather, non-behaviour).<\/p>\n<p>I suggest something along the lines of:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  procedure TApplication.AddForm(const aFormClass: TComponentClass;\r\n                                 const aReferenceVar: PObject);\r\n<\/pre>\n<p>Which would then have been called like this:<\/p>\n<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\n  Application.AddForm(TMyForm, @MyForm);\r\n<\/pre>\n<p>You would not then have needed the very smelly &#8220;RealCreateForms&#8221; method (any method which suggests that it is <em>really<\/em> doing something in place of some other thing which gave the impression of having done something but really didn&#8217;t is strongly suggestive to me of code that needs some serious re-thinking).<\/p>\n<p>Or rather, you would have had a method you could call more simply and honestly&#8230; <strong>CreateForms<\/strong>.<\/p>\n<p>i.e. two methods which both do what they say, rather than the current situation of one method which doesn&#8217;t do what it says and another method whose name contains an apology for the poor behaviour of it&#8217;s related sibling.<\/p>\n<p>Two things would make this better than what we actually have imho:<\/p>\n<ol>\n<li>It is different from what we had (and <strong>still<\/strong> have) in the VCL, which is good and proper because it is &#8211; after all &#8211; <em>doing<\/em> something quite different too!<\/li>\n<li>The parameter list is more clearly expressing the behaviour of the method.  It does not modify the reference variable provided and asked for, it is asking for a <em>pointer<\/em> to a reference variable.\n<p>It is far more readily apparent that whatever is going to happen to that reference is not going to occur <em>now<\/em> but at some later point.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">[Estimated Reading Time: <\/span> <span class=\"rt-time\">2<\/span> <span class=\"rt-label rt-postfix\">minutes]<\/span><\/span> Apparently, when your application is a FireMonkey application.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[4,143],"tags":[135,136],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1TKYv-cP","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":791,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/791\/","url_meta":{"origin":795,"position":0},"title":"Why Does My OSX FireMonkey App Think It Is a Console App ?","date":"15 Sep 2011","format":false,"excerpt":"Because System.IsConsole is hardwired to TRUE on MAC OS: This is actually also the case for $ifdef LINUX, but nobody is likely to notice that, at least not just yet. :) But it caused me no end of confusion when my FireMonkey app behaved one way on Windows and a\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":735,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/735\/","url_meta":{"origin":795,"position":1},"title":"RAD STUDIO XE2: Launch Event Report","date":"04 Aug 2011","format":false,"excerpt":"Today I was fortunate to be present in Auckland at the World Premier of the launch event for RAD Studio XE2. \u00a0There is so much good to report that I really don't know where to begin, so apologies if this post is a bit of a disorganised ramble. \u00a0But here\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1068,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/1068\/","url_meta":{"origin":795,"position":2},"title":"Info From the World Tour (Hamburg)","date":"22 Aug 2012","format":false,"excerpt":"In the Embarcadero forums, Roland Kossow posted his report on the first of the \"RAD Studio World Tour\" events in Hamburg yesterday, reporting on what's new in XE3 and adding some more detail to the \"XE3 And Beyond\" blog post. In a nutshell we have \"FM2\" (FireMonkey 2 - no\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":750,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/750\/","url_meta":{"origin":795,"position":3},"title":"XE2 FireMonkey Designer &#8211; Clipboard Gotcha","date":"06 Sep 2011","format":false,"excerpt":"We are still waiting for our XE2 licenses to be organised but in the meantime I have started playing around with the Free Trial Edition. I haven't had much time to form much of an opinion as yet, but have already identified some oddities in the FireMonkey Form Designer, specifically\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.deltics.co.nz\/blog\/wp-content\/uploads\/Screen-Shot-2011-09-06-at-9.48.39-AM.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":924,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/924\/","url_meta":{"origin":795,"position":4},"title":"CFStringRef and TCFString(s)","date":"05 Jul 2012","format":false,"excerpt":"In the comments on yesterdays initial post in a series following the experiences of porting an Objective-C sample to XE2, a number of people have asked why I didn't use the TCFString record type in System.Mac.CFUtils to get the CFStringRef references that I required. The reason is embarrassingly simple. I\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":843,"url":"https:\/\/www.deltics.co.nz\/blog\/posts\/843\/","url_meta":{"origin":795,"position":5},"title":"XE2 Update #4 On The Horizon","date":"19 Jan 2012","format":false,"excerpt":"The release notes for Update #4 have appeared online, signalling the imminent arrival no doubt of the update itself. The good news if you are using FireMonkey is that this is a significant update, including functionality for printing. The bad news is that like update #1, this update is not\u2026","rel":"","context":"In &quot;Delphi&quot;","img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/795"}],"collection":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=795"}],"version-history":[{"count":4,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/795\/revisions"}],"predecessor-version":[{"id":799,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/posts\/795\/revisions\/799"}],"wp:attachment":[{"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.deltics.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}