{"id":41,"date":"2007-01-09T03:00:00","date_gmt":"2007-01-09T11:00:00","guid":{"rendered":"http:\/\/testblog.sapien.com\/index.php\/2007\/01\/09\/execute-iii\/"},"modified":"2007-01-09T03:00:00","modified_gmt":"2007-01-09T11:00:00","slug":"execute-iii","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2007\/01\/09\/execute-iii\/","title":{"rendered":"Execute! III"},"content":{"rendered":"<p><P>Okay, last time, I left you with two functions which were nearly identical. This one:<\/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>This function returns a nonzero value.&nbsp;Although&nbsp;r has a value of zero in the global scope, function Main() also declares r and assigns a nonzero value. The local version of r&nbsp;overrides the global r within the function; because the expression is being executed within the function, r hs a value and the result is nonzero.&nbsp;I also gave you a&nbsp;near-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><FONT face=\"Times New Roman,Times,serif\">In this case, the mathematical expression is being executed in the global scope, which essentially means it&#8217;s being raised up out of the function. In the global scope, r has a value of zero, so the result of the expression is zero. There&#8217;s another problem, too: When the expression executes globally, the variable result is implicitly declared in the global scope. That variable was declared explicitly within the function, though, so the function&#8217;s version of &#8220;result&#8221; is different from the global scope&#8217;s version of &#8220;result.&#8221; That means the local version of &#8220;result&#8221; is never being assigned a value <EM>within the local scope, <\/EM>so the function will<\/FONT><EM> always<\/EM> return a zero value.<\/P><P>This little series serves to illustrate several important truths about scope in VBScript:<\/P><P>First, globally-declared variables are accessible everywhere.<\/P><P>Second, locally-declared variables (within a function or sub) or only available within their local scope.<\/P><P>Third, a locally-declared variable will, within a local scope, have precedence over a globally-declared variable of the same name.<\/P><P>Fourth, Option Explicit is your friend :). <\/P><P>Fifth, although you may run ExecuteGlobal within a local scope, whatever code you&#8217;re executing executes within the global scope, not the local scope.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p><P>Okay, last time, I left you with two functions which were nearly identical. This one:<\/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>This function returns a nonzero value.&nbsp;Although&nbsp;r has a value of zero in the global scope, function Main() &#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-41","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\/41","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=41"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}