Multiple Commands on a Single Line in VBScript

tigermattSite Reliability Engineer
Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy code, with declarations for variables, and then actually setting those variables, using up two lines, like the below.

Dim myVariable
Set myVariable = Server.CreateObject("MyObject")
Dim myString
myString = Request.ServerVariables("name")
Dim myInt
myInt = 44-6

Open in new window

Fortunately, there is help at hand. A little known trick to many scripting in VBS is the power of the colon - this simple character enables you to place multiple commands on a single line, replacing the location where you would place a line break with a colon.

Using this trick, my code above now looks like:
Dim myVariable: Set myVariable = Server.CreateObject("MyObject")
Dim myString: myString = Request.ServerVariables("name")
Dim myInt: myInt = 44-6

Open in new window

As you can see, this code is a lot cleaner; the colon character has been used to replace the line break between a variable declaration and setting that variable's value, enabling easier reading of code for both yourself and future developers.

tigermattSite Reliability Engineer

Comments (5)

b0lsc0ttIT Manager

Great article!  Thanks for letting us know about that wonderful character and its use (or reminding those that may have used it in VB).  Now the key is to remember it the next time I need it. :)

Thanks for the time to make a nice looking and informative article.

tigermattSite Reliability Engineer
Most Valuable Expert 2011



I appreciate that technically, under the various guidelines for an article, this article doesn't come up to scratch. However, there's no way I can feasibly see of enhancing this to make it come up to the guidelines. The article is succinct: it defines a problem (messy code in VBScript), provides a way of resolving this issue (the colon character), and also explains when you would use this (and the equivalents in other languages).

The various article guidelines are really that: guidelines. As I have been saying for some time, an article may not come up to scratch under those guidelines, but may end up being the best article on the site. It's all in the way the article is written.



Great snippett that a busload of people including me I'm sure are unaware of....good onya Mr Tigermatt!!!
bivesst2:  Make sure you click the good old "Yes" it was helpful link then to give TigerMatt his props!
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

I might add that you should use reasonable spacing if you combine commands on one line. In above code, it would look like

Dim myVariable:   Set myVariable = Server.CreateObject("MyObject")
Dim myString:       myString = Request.ServerVariables("name")
Dim myInt:             myInt = 44-6

which should attract the reader to the important parts, which are assignments.
You can even align by the equal sign by using more spaces in VB Script, but in VBA it does not work because of the automatic formatting the editor applies.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community