{"id":615,"date":"2022-08-30T15:04:18","date_gmt":"2022-08-30T15:04:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/the-correct-actionscript-to-find-if-object-already-exists-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:18","modified_gmt":"2022-08-30T15:04:18","slug":"the-correct-actionscript-to-find-if-object-already-exists-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/the-correct-actionscript-to-find-if-object-already-exists-collection-of-common-programming-errors\/","title":{"rendered":"the correct actionscript to find if object already exists-Collection of common programming errors"},"content":{"rendered":"<p>I have a Rect object that I&#8217;d like to create and set its properties only once. After that, I want to just modify its properties since it already exists. This is my general idea<\/p>\n<pre><code>if(theRect == undefined){\n\n  Alert.show(\"creating\");\n\n  var theRect:Rect = new Rect();\n  \/\/then set properties\n  addElement(theRect); \/\/then add it using addElement because addChild() does not work\n\n} else {\n\n  Alert.show(\"updating\");\n\n  \/\/no need to create it since it's already been created\n  \/\/just access and change the properties\n\n}\n<\/code><\/pre>\n<p>I tried several ways and combinations for the if conditional check:<\/p>\n<pre><code>if(theRect == undefined){\nif(theRect == null){\ndeclaring and not declaring `var theRect:Rect;` before the if check\ndeclaring and instantiating to null before the if check `var theRect:Rect = null;` \n<\/code><\/pre>\n<p>but can&#8217;t get the desired effect. Everytime this code block runs, and depending on which version I&#8217;ve used, it either gives me a &#8220;can&#8217;t access null object&#8221; error or the if statement always evaluates to true and creates a new Rect object and I get the &#8220;creating&#8221; Alert.<\/p>\n<p>What&#8217;s the right way of creating that Rect but only if doesn&#8217;t exist?<\/p>\n<ol>\n<li>\n<p>You have some scoping issues in the code you presented.<\/p>\n<p>What I think you want to do is:<\/p>\n<pre><code>var theRect:Rect;\n\n...\n\nif(theRect == null)\n{\n    theRect = new Rect();\n    ...\n}\n...\n<\/code><\/pre>\n<p>You need to first declare theRect, but you don&#8217;t have to create it. You can instantiate it lazily later by checking if it is null.<\/p>\n<p>The way you have it set up, you created a local version of theRect inside your <code>if<\/code> statement that would not be visible elsewhere. You will also get an error trying to access <code>theRect<\/code> if you did not declare it beforehand.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:07:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a Rect object that I&#8217;d like to create and set its properties only once. After that, I want to just modify its properties since it already exists. This is my general idea if(theRect == undefined){ Alert.show(&#8220;creating&#8221;); var theRect:Rect = new Rect(); \/\/then set properties addElement(theRect); \/\/then add it using addElement because addChild() does [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-615","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}