{"id":34,"date":"2007-01-02T02:00:00","date_gmt":"2007-01-02T10:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/02\/execute-ii\/"},"modified":"2007-01-02T02:00:00","modified_gmt":"2007-01-02T10:00:00","slug":"execute-ii","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/02\/execute-ii\/","title":{"rendered":"Execute! II"},"content":{"rendered":"<p><P>Happy New Year!<\/P><P>When last we spoke, I&#8217;d given you some code that doesn&#8217;t work quite right:<\/P><P><FONT face=\"Courier New\">Const pi = 3.1416<BR>WScript.Echo Main()<BR>Function Main()<BR>&nbsp;Const r = 2.5<BR>&nbsp;Dim result<BR>&nbsp;Dim str<BR>&nbsp;str = &#8220;result=pi*(r*r)&#8221;<BR>&nbsp;ExecuteGlobal str<BR>&nbsp;Main = result<BR>End Function<\/FONT><\/P><P>Did you see why? ExecuteGlobal is executing the contents of variable str <EM>in the global scope. <\/EM>However, the constant r doesn&#8217;t exist in the global scope &#8211; it&#8217;s local to the function Main(). In the global scope, then, VBScript has to implicitly declare a variable r and give it the default value of zero. The result of the mathematical expression is therefore zero, since anything multiplied by zero results in zero. Had I put Option Explicit in there, I would have gotten an error when running ExecuteGlobal, since I would have been executing code that used an undeclared variable &#8211; at least, undeclared in the scope in which the code executed. <\/P><P>There&#8217;s a second problem, too, which is that when ExecuteGlobal runs, a variable named &#8220;result&#8221; is being implicitly declared in the global scope, because that expression is using the variable &#8220;result&#8221; in the global scope, where the variable hasn&#8217;t been explicitly declared. Again, Option Explicit would have generated an error here. The function&#8217;s copy of &#8220;result&#8221; remains at zero, because <EM>nothing in the function&#8217;s local scope ever assigned a value to the local version of &#8220;result.&#8221; <\/EM>This is a big reason to NEVER re-use variable names in the global scope and the local scope.<\/P><P>Here&#8217;s another one to examine:<\/P><P><FONT face=\"Courier New\">Const pi = 3.1416<BR>r = 0<BR>WScript.Echo Main()<BR>Function Main()<BR>&nbsp;r = 2.5<BR>&nbsp;Dim result<BR>&nbsp;Dim str<BR>&nbsp;str = &#8220;result=pi*(r*r)&#8221;<BR>&nbsp;Execute str<BR>&nbsp;Main = result<BR>End Function<\/FONT><\/P><P>And its twin:<\/P><P><FONT face=\"Courier New\">Const pi = 3.1416<BR>r = 0<BR>WScript.Echo Main()<BR>Function Main()<BR>&nbsp;r = 2.5<BR>&nbsp;Dim result<BR>&nbsp;Dim str<BR>&nbsp;str = &#8220;result=pi*(r*r)&#8221;<BR>&nbsp;ExecuteGlobal str<BR>&nbsp;Main = result<BR>End Function<\/FONT><\/P><P>Do you see what&#8217;s logically wrong, here? One of these will return zero; the other will return an actual value. Why? Well, there are two good reasons: More details next Tuesday&#8230;<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p><P>Happy New Year!<\/P><P>When last we spoke, I&#8217;d given you some code that doesn&#8217;t work quite right:<\/P><P><FONT face=\"Courier New\">Const pi = 3.1416<BR>WScript.Echo Main()<BR>Function Main()<BR>&nbsp;Const r = 2.5<BR>&nbsp;Dim result<BR>&nbsp;Dim str<BR>&nbsp;str = &#8220;result=pi*(r*r)&#8221;<BR>&nbsp;ExecuteGlobal str<BR>&nbsp;Main = result<BR>End Function<\/FONT><\/P><P>Did you see why? ExecuteGlobal is executing the contents of variable str <EM>in the global scope. <\/EM>However, &#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,24],"tags":[],"class_list":["post-34","post","type-post","status-publish","format-standard","hentry","category-general","category-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/34","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=34"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/34\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=34"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=34"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=34"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}