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.