<

Multiple Commands on a Single Line in VBScript

Published on
30,350 Points
23,550 Views
8 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
8
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 72

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Join & Write a Comment

SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month