Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about ":" use in script 1

Status
Not open for further replies.

mharcourt

Programmer
Apr 9, 2003
49
0
0
US
While reviewing some sample code I noticed the use of ":" in the script. Once in the first DIM statement and then again in the "Set products" statement. I can not find any documentation on the use of ":". See Code below. And ideas?

Code:
' Connect to Windows Installer object
On Error Resume Next
Dim installer : Set installer = Nothing
Dim product
Dim products
Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError

'Enumerate through all registered products, checking the installstate
Set products = installer.Products : CheckError
For Each product In products
	' Only apps that have nothing installed will pass this check. 
	If (installer.ProductState(product) = msiInstallStateAdvertised) Then
		Wscript.echo installer.ProductInfo( product, "ProductName") & " Is Advertised"
		InstallLocal product
	Else
		Wscript.echo installer.ProductInfo( product, "ProductName") & " Is Installed" 
	End If
Next
Wscript.Quit 0

Sub InstallLocal( Product )
	Wscript.echo "Installing " & installer.ProductInfo( product, "ProductName")

	'Set Basic UI, wiht no modal dialogs
	installer.UILevel = msiInstallUILevelBasic + msiInstallUILevelProgressOnly: CheckError

	'Install the product fully (max feature level) 
	installer.ConfigureProduct product, 65535, msiInstallStateLocal : CheckError 
End Sub


Sub CheckError
	Dim message, errRec
	If Err = 0 Then Exit Sub
		message = Err.Source & " " & Hex(Err) & ": " & Err.Description
	If Not installer Is Nothing Then
	Set errRec = installer.LastErrorRecord
		If Not errRec Is Nothing Then 
			message = message & vbNewLine & errRec.FormatText
			Wscript.Echo message
			Exit Sub
		End If
	End If
	Wscript.Echo message
	'Wscript.Quit 2
End Sub
 

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
bascially translated it is a short cut to declarations
so if you did
Dim str : str = "test"
alert str

is the same as saying
Dim str
str = "test"
alert str

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
onpnt:
The page you linked to is for the JavaScript "conditional" operator, also called the ternary operator in C. VBScript doesn't have this operator and doesn't use the colon in this way. The closest thing to this is the IIf function in VBA.

Also, you mention that the colon is a short cut for declaring variables, but its really much more general than that. The colon is primarily used in VBScript to allow putting multiple statements on one line. This is sort of the opposite of the underscore character which allows breaking one statement over multiple lines.

In VBA and VB the colon is also used to define a label as the target of a Goto or On Error statement, but VBScript doesn't have this capability.

mharcourt:
I found some descriptions of using the colon as a multi-statement separator in the VBScript help file by searching for "colon". It doesn't seem to be described in its own topic otherwise.

In the script you posted, it's used frequently to call the CheckError routine after calls to the installer object. The author could have put each CheckError statement on its own line with the same result.

I think use of the colon to group statements on a line is mostly personal preference. I'm not a fan of it myself, since I would have trouble keeping track of statements that start well into a line, but an often repeated call like in the installer script seems like a reasonable way to use it. I've never used the VBScript debugger, but I know that some debuggers are line oriented and wouldn't see the second or following statements on a line, so its use may be somewhat deprecated for that reason.

-xift
 
ya, just noticed the I pasted the wrong link
the link was for WMLScript (language aside) conditional operators.

____________________________________________________
[sub]The most important part of your thread is the subject line.
Make it clear and about the topic so we can find it later for reference. Please!! faq333-3811[/sub]
onpnt2.gif
 
Thanks for all the replies. I think I'll forgo on using the : , mostly because I'll probably won't remember want it does if I need to modify scripts and if someone else needs to modify my scripts.
 
I'm not sure what the big deal is here.

Nearly every MS Basic I can think of (well, all that I can think of) supports the colon ":" character as a statement separator within a line of source text. It doesn't get used a lot because it is considered "bad form" like the use of GOTOs (which is NOT supported in VBScript except as part of the error trap shut-off "On Error GoTo 0").

There isn't an explicit reference I can find in the VBScript docs, but there is an implicit one under the If statement:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top