<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Multiple Commands on a Single Line in VBScript

Published on
31,560 Points
24,660 Views
9 Endorsements
Last Modified:
Approved
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.

-Matt
9
Comment
Author:tigermatt
5 Comments
LVL 54

Expert Comment

by:b0lsc0tt
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.

bol
0
LVL 58

Author Comment

by:tigermatt
bol,

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.

Thoughts?

-Matt
0

Expert Comment

by:bivesst2
Great snippett that a busload of people including me I'm sure are unaware of....good onya Mr Tigermatt!!!
0
LVL 39

Expert Comment

by:BrandonGalderisi
bivesst2:  Make sure you click the good old "Yes" it was helpful link then to give TigerMatt his props!
0
LVL 73

Expert Comment

by:Qlemo
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.
0

Featured Post

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Join & Write a Comment

Please check the video also in regards to recovery of deleted emails from office 365 admin center and through the MFCMAPI tool. I have mentioned each and every step with the proper steps that need to be taken care of.
Microsoft Office 365 Backup and Restore Solution by SysTools to export Office 365 mailbox to PST / EML file format on Windows OS. On Mac, tool backup O365 to PST / MBOX / MSG / EML / EMLX file formats. Not only this, restore option helps to import s…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month