<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx</link><description>A reader asked me the other day to riff a bit on why this works: Dim i For i = 1 To 2 print c Next Const c = 10 And this works For i = 1 To 2 print c Dim i Next Const c = 10 but this fails with a "name redefined" error: For i = 1 To 2 print c Const c</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278264</link><pubDate>Wed, 08 Dec 2004 14:13:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278264</guid><dc:creator>Jiho Han</dc:creator><description>So in these statements,&lt;br&gt;&lt;br&gt;print TypeName(c)&lt;br&gt;print Eval(&amp;quot;TypeName(c)&amp;quot;)&lt;br&gt;Const c = 10&lt;br&gt;&lt;br&gt;So on which line does the slot 'c' get created? on the first or the second? or is it both lines since they are in a different context?</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278292</link><pubDate>Wed, 08 Dec 2004 14:54:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278292</guid><dc:creator>Mat Hall</dc:creator><description>I think we should go back to line numbers.  At least then you KNOW what order things are going to happen in!&lt;br&gt;&lt;br&gt;(Unless you use FORTRAN, in which case line numbers make no difference to execution order.)</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278352</link><pubDate>Wed, 08 Dec 2004 16:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278352</guid><dc:creator>Eric Lippert</dc:creator><description>The second.  As you can see from the code gen, the first line is exactly equivalent to 'print typename(10)'.&lt;br&gt;</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278537</link><pubDate>Wed, 08 Dec 2004 22:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278537</guid><dc:creator>Nicholas Allen</dc:creator><description>From a design standpoint, was there an advantage to not hoisting the allocation of a slot for a constant to the top of scope?  Or did noone notice that it was a problem until it was too late to go and change it?&lt;br&gt;&lt;br&gt;By the way, thanks for the conversion help the other day.  By bad design indeed, unfortunately.</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278576</link><pubDate>Wed, 08 Dec 2004 23:07:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278576</guid><dc:creator>Eric Lippert</dc:creator><description>I'm pretty sure that this was a &amp;quot;no one noticed it&amp;quot; issue.  But that particular design decision predates my time here, so I can't be sure.</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278599</link><pubDate>Wed, 08 Dec 2004 23:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278599</guid><dc:creator>DWalker</dc:creator><description>Shouldn't &amp;quot;Call Print 1&amp;quot; be &amp;quot;Call Print 10&amp;quot; instead? Otherwise, something is REALLY wrong. &lt;br&gt;&lt;br&gt;This, on the other hand&lt;br&gt;&lt;br&gt;print c&lt;br&gt;Const c = 10&lt;br&gt;&lt;br&gt;generates this code:&lt;br&gt;&lt;br&gt;Bos 0 &lt;br&gt;IntConst 10 &lt;br&gt;Call 'print' 1 &lt;br&gt;Bos 1 &lt;br&gt;IntConst 10 &lt;br&gt;ConstSt 'c' &lt;br&gt;</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#278607</link><pubDate>Thu, 09 Dec 2004 00:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:278607</guid><dc:creator>Eric Lippert</dc:creator><description>&amp;gt; Shouldn't &amp;quot;Call Print 1&amp;quot; be &amp;quot;Call Print 10&amp;quot;&lt;br&gt;&lt;br&gt;No.  Read my comments above again:&lt;br&gt;&lt;br&gt;LocalAdr 1             put reference to slot 1 on stack&lt;br&gt;Call 'print' 1         call print with one argument&lt;br&gt;&lt;br&gt;So &amp;quot;IntConst 10&amp;quot; puts 10 on the argument stack, &amp;quot;Call print 1&amp;quot; calls print and passes one argument from the argument stack.  &lt;br&gt;&lt;br&gt;If it had been &amp;quot;print 10, 20&amp;quot; then it would have been&lt;br&gt;&lt;br&gt;IntConst 10&lt;br&gt;IntConst 20&lt;br&gt;Call 'print' 2&lt;br&gt;&lt;br&gt;Make sense?&lt;br&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#450912</link><pubDate>Fri, 12 Aug 2005 19:37:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:450912</guid><dc:creator>Anthonyt</dc:creator><description>I want to do this:&lt;br&gt;&lt;br&gt;    dim strValue&lt;br&gt;    strValue = &amp;quot;Hello World&amp;quot;&lt;br&gt;    const MY_CONST = &amp;quot;Hi&amp;quot; &amp;amp; strValue&lt;br&gt;&lt;br&gt;But it doesn't work. Is there a way around it? I need to take the string value of the variable and put it into the constant.</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#452531</link><pubDate>Wed, 17 Aug 2005 09:34:02 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:452531</guid><dc:creator>Eric Lippert</dc:creator><description>Read the link &amp;quot;how constants work in VBScript&amp;quot; above for the answer to your question.&lt;br&gt;</description></item><item><title>re: VBScript Constants Are Not Hoisted</title><link>http://blogs.msdn.com/ericlippert/archive/2004/12/07/277763.aspx#1764585</link><pubDate>Mon, 26 Feb 2007 21:22:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1764585</guid><dc:creator>Seamus</dc:creator><description>&lt;p&gt;Thanks so much for posting this info on how constants are evaluated, it's been very very useful. &amp;nbsp;Given that constants are not hoisted and they are handled using very limited constant folding, is there any possibility that same named constants could unintentionally get their values crossed? &amp;nbsp;For example:&lt;/p&gt;
&lt;p&gt;Given the following .asp files&lt;/p&gt;
&lt;p&gt;common/common.asp:&lt;/p&gt;
&lt;p&gt;--------------&lt;/p&gt;
&lt;p&gt;Sub printConstValue&lt;/p&gt;
&lt;p&gt; &amp;nbsp; response.write &amp;quot;Const Value: &amp;quot; &amp;amp; MY_CONST&lt;/p&gt;
&lt;p&gt;End Sub&lt;/p&gt;
&lt;p&gt;app_A/page_A.asp:&lt;/p&gt;
&lt;p&gt;-----------------&lt;/p&gt;
&lt;p&gt;&amp;lt;!--#INCLUDE FILE=&amp;quot;../common/common.asp&amp;quot; --&amp;gt;&lt;/p&gt;
&lt;p&gt;const MY_CONST = &amp;quot;A&amp;quot;&lt;/p&gt;
&lt;p&gt;app_A/page_A1.asp:&lt;/p&gt;
&lt;p&gt;-----------------&lt;/p&gt;
&lt;p&gt;&amp;lt;!--#INCLUDE FILE=&amp;quot;page_A.asp&amp;quot; --&amp;gt;&lt;/p&gt;
&lt;p&gt;call printConstValue&lt;/p&gt;
&lt;p&gt;app_B/page_B.asp:&lt;/p&gt;
&lt;p&gt;-----------------&lt;/p&gt;
&lt;p&gt;&amp;lt;!--#INCLUDE FILE=&amp;quot;../common/common.asp&amp;quot; --&amp;gt;&lt;/p&gt;
&lt;p&gt;const MY_CONST = &amp;quot;B&amp;quot;&lt;/p&gt;
&lt;p&gt;app_B/page_B1.asp:&lt;/p&gt;
&lt;p&gt;-----------------&lt;/p&gt;
&lt;p&gt;&amp;lt;!--#INCLUDE FILE=&amp;quot;page_B.asp&amp;quot; --&amp;gt;&lt;/p&gt;
&lt;p&gt;call printConstValue &lt;/p&gt;
&lt;p&gt;Sometimes calling printconstValue from page B will print out const value declared in file A? &amp;nbsp;I have situation similar to this and I've been scratching my head trying to figure out why every now and then the values of the constants are getting &amp;quot;crossed&amp;quot;. &amp;nbsp; &lt;/p&gt;
</description></item></channel></rss>