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

…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 
, and web browsers honor hexadecimal codes in URLs, VBScript does neither.  So these also don’t work:

CONST blah =“hello 

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.

Bookmark the permalink.

3 Responses to Answered: line-breaks in VBScript constants

  1. Eric Lippert says:

    I would not say that I wrote "most" of VBScript. I joined Microsoft in July of 1996 when the first version of VBScript was just about ready to ship. I didn’t write the lexer, parser, code generator, bytecode interpreter or runtime library — but I certainly did make extensive modifications to them over the year.

  2. Andy says:

    you can use chr 13 & 10 as a replacement, in my code below i ask users to enter a $ symbol to indicate they want a line break

    strOOONewMessage = Replace(strOOONewMessage, "$", chr(13)&chr(10))

    this works when running a .vbs script on the desktop to collect text from an inputbox and then pump the line into Exchange for an out of office message

  3. Pingback: Javascript -vs- VBScript, pros and cons compared - Rob Eberhardt

Leave a Reply

Your email address will not be published. Required fields are marked *