Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


April 09, 2009

Script Trek

RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!


Captain's log, star date 2856.9.

The Enterprise is trapped in a space-time vortex. As a result, the ship's engine has been malfunctioning. Lieutenant Commander Montgomery Scott, our chief engineer, created a script to re-adjust the engine's parameters and free Enterprise from the vortex. The script didn't produce the expected results.

McCoy: Scotty said that if we don't escape the vortex in the next hour, the Enterprise will spin out of control and we'll be lost in space.

Kirk: Lost In Space?

McCoy: That's what he said, Jim.

Kirk: But that's the wrong TV series!

McCoy: Exactly.

Kirk: Is there anything we can do to avoid this?

McCoy: I don't know, Jim. I'm a doctor, not a systems engineer.

Kirk: Uhura, open a Unified Communications channel with Mr. Scott in the engine room.

Uhura: Unified Communications channel opened, Captain.

Spock: I just returned from the engine room. The VBScript script is behaving erratically, almost illogically.

Kirk: Scotty, how's the situation?

Scott: I have good news, Captain. I found the problem area in my script. I created a smaller proof-of-concept script to demonstrate the problem.

Kirk: Show us this script in our monitors, Scotty.

Scott: Here it is, Captain.

Option Explicit
Dim a, b
a = InputBox("Input an integer value")
b = a + 1 
If a < b Then
  MsgBox "This message box should always appear"
End If
MsgBox "End of test"

When I run this script, the message box doesn't appear. I'm starting to think there is something wrong not with my script but with VBScript in general.

Chekov: If we change line

If a < b Then

to

If CInt(a) < CInt(b) Then

will the script work?

Scott: Wait a minute... Yes, Mr. Chekov, indeed the script works now!

Uhura: Or, better yet, change the InputBox line to

a = CInt(InputBox("Input an integer value"))

Kirk: Can you show us the final version of the script in our monitors, Scotty?

Scott: Here it is.

Option Explicit
Dim a, b
a = CInt(InputBox("Input an integer value"))
b = a + 1 
If a < b Then
  MsgBox "This message box should always appear"
End If
MsgBox "End of test"

Kirk: You can use the TypeName function to see what type VBScript assigned for a variable.

Scott: Interesting, Captain. I'm checking this right now. I added the line

MsgBox TypeName(a)

right after the InputBox line in my first script. I see now that TypeName function says the a variable is a string.

McCoy: That's probably because you used the InputBox function to assign a value to the variable. It seems that VBScript will consider anything the InputBox function returns as a string.

Kirk: Maybe you've got a little bit of an engineer in you after all, Bones.

Chekov: Mr. Scott, if you had used PowerShell instead of VBScript, is there a chance that you would've had the same problem? In VBScript, all declared variables are of type variant. But in PowerShell, you don't declare variables explicitly. Variables are implicitly declared when they're first assigned a value.

Scott: Let's see. In PowerShell, the script would be as follows:

Set-PSDebug -Strict
$a = Read-Host "Input an integer value"
$b = $a + 1 
if ($a -lt $b) {Write-Host "This message should always appear"}
Write-Host "End of test"

Let me try it. Well, this script works correctly! PowerShell assigns the correct type to the variables, at least in this case. So, had I used PowerShell, my script would have produced the correct results.

Chekov: You're correct, but it's still better to denote the type, like this:

Set-PSDebug -Strict
[int]$a = Read-Host "Input an integer value"
[int]$b = $a + 1 
if ($a -lt $b) {Write-Host "This message should always appear"}
Write-Host "End of test"

Because PowerShell variables aren't declared beforehand, it would be best to cast them to their intended type.

Uhura: Is this like a C-type cast?

Kirk: Yes. As you can see, the only difference is that instead of parentheses, we have brackets here. And these casts are not unlike the typecast effect these roles have had in our acting careers.

Chekov: But we have to be careful, even with PowerShell.

Spock: Indeed. You never know how PowerShell is going to view your variables. The best thing you can do is to explicitly cast them. I've never understood the idea behind scripting environments.

McCoy: Am I on "Punk'd"? What exactly haven't you understood, you emotionless Vulcan?

Spock: Live-long-and-prosper to you, too. And to answer your question, it's illogical to use a weakly typed language instead of a strongly typed one.

McCoy: Scripting and dynamically typed languages provide great ease and convenience.

Spock: Ease? And convenience? At the expense of safety?

McCoy: You should be able to declare the variables you use and their types and not leave it to the compiler or interpreter to decide. I give you that. VBScript has Option Explicit and PowerShell has Strict Mode to enforce variable declaration. To denote the type of a variable, VBScript has type conversion functions such as CInt, and in PowerShell you use square brackets to typecast as we've just seen. And if you want to find a variable's type, you use the TypeName function in VBScript and the GetType method in PowerShell. So we're good to go. What? You don't like it because it's not "pure", right?

Kirk: Enlightening as this may be, it's time to check the results. Scotty, can you run your script with the corrections?

Scott: I have just used type conversion functions wherever I have input boxes, and it seems to work. The results look legitimate now.

Kirk: OK. Now restart the engine with the new parameters.

Scott: Yes, Captain. The engine is now operational. The Enterprise is freed from the vortex!

Kirk: Excellent job, Scotty. You know, this could be as important as moving to syndication. Ahead, warp factor one, Mr. Sulu.

Sulu: Warp factor one, sir. And thank you for my small part in this episode, sir. I had no time to prepare for a bigger part now with my appearances in "Heroes" and all...

THE END

—Dimitrios Kalemis, systems engineer

End of Article



Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...


Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement