-
Recent Posts
Recent Comments
- David Jones on GPO To Set Firewall Exception For Windows 10 RDP
- Greg Woods on GPO To Set Firewall Exception For Windows 10 RDP
- Neil on GPO To Set Firewall Exception For Windows 10 RDP
- Gopal Sharma on GPO To Set Firewall Exception For Windows 10 RDP
- Rob Eberhardt on GPO To Set Firewall Exception For Windows 10 RDP
Archives
- February 2022
- July 2020
- November 2018
- October 2018
- May 2017
- April 2016
- March 2016
- November 2015
- October 2015
- September 2015
- June 2015
- May 2014
- August 2013
- July 2013
- March 2013
- August 2012
- June 2012
- May 2012
- February 2011
- December 2010
- November 2010
- October 2010
- August 2010
- June 2010
- October 2009
- September 2009
- May 2009
- April 2009
- March 2009
- September 2008
- August 2008
- July 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- July 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- May 2006
- April 2006
- March 2006
- February 2006
- January 2006
- December 2005
- November 2005
- October 2005
- September 2005
- August 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- February 2005
- January 2005
Categories
Meta
Monthly Archives: March 2005
Answered: line-breaks in VBScript constants
I’ve run into this often: the need to have a VBScript constant containing a line-break character. Today I finally got a definitive answer on whether it’s possible (for the impatient, the answer is NO).
If you’re interested in history…
Of course this is right out:
CONST blah = “hello
there”
…It’s just bad syntax. The closing string quote has to be on the same line as the opening one.
The normal approach I’ve tried was this:
CONST blah = “hello ” & vbCRLF & ” there”
..But the ampersand (concatenation operator) automatically makes it an expression to the VBScript compiler, and therefore it assumes “not constant.” This is of course despite the fact that both parts are known at the time of compilation (which is the main criterion for a constant — value is known at compile time). Anyway, the ampersand is right out.
Now in JScript/Javascript/ECMAScript, you can do this:
var blah = “hello \r\n there”
…The \r\n switches define the line-break character, they go inside the string, and they are only interpreted when it’s read.
Unfortunately, there’s no similar switch in VBScript. While HTML does honor ASCII codes like or , and web browsers honor hexadecimal codes in URLs, VBScript does neither. So these also don’t work:
CONST blah =“hello there”
CONST blah = “hello %0A%0D there”
CONST blah = “hello 0x0A0x0D there”
So I was reading a blog entry by Eric Lippert (who I believe wrote most of VBScript) today, and he mentioned Constant Folding, and proceeded to outline the mechanics behind this VBScript constant problem. It rung a bell, I asked about line-breaks in VBScript constants in his comments, and Eric responded:
“Sorry, you are correct — there’s no way to do that. Sub-optimal, I know. “
…Straight from the dev’s mouth.
SO, if you need a line-break in a VBScript constant, just use a variable instead:
DIM blah
blah = “hello ” & vbCRLF & ” there”
…Yes, there may be some slight performance penalty compared to a constant, but it is script after all — racing performance ain’t the point.