Archive

Archive for November, 2009

Line Break in VBScript

November 16th, 2009 No comments

When doing the VBScript programming, it often need to have a VBScript constant containing a line-break character. 

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 normally you would try 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, if you need a line-break in a VBScript constant, just use a variable instead:

DIM blah
blah = "hello " & vbCRLF & " there"
Categories: Development, VB .Net Tags: