To DIM or not to DIM that is the question, VBScript Variables

So I got an email from someone asking me based on the code snippets I place on my site why don’t I “Option Explicit” and declare my variables….

So what are variables and what do you do with them?
I’m not going to write to much but pull from another location – Source
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).

So now that you know what variables are, let’s talk about variable type’s in VBScript’s and why I don’t use them…

In VBScript, all variables are of type variant, that can store different types of data, unlike other languages like VB that you need to declare the variable type like Integer, Decimal etc
You can declare variables in VBScript by starting you code with

Option Explicit
Dim MySpaceShip
MySpaceShip = "Some Odd Name or Value"

So in VB you would need to declare what your variable would hold something like

Dim MySpaceShip As String ' --> While string can be any value of characters and numbers....
Dim MySpaceShipSpeed As Integer ' --> My Space Ship's speed can only be in number values.....

There are more values but I won’t get into that as I said VBScript variables are always variant’s that can hold any value numeric or alphanumeric.

Some Rules
A variable can have a short name, something like Y would work but you should put some more details, let’s say MySpaceShip.
Rules for VBScript variable names:
They have to begin with a letter!
The varicable cannot contain a period “.”, the value they hold can
Length max is only 255 long

So back to VBScript and DIMing……
Why don’t I? the answer is quite simple, I create all scripts with VBS Edit and I don’t feel the need to declare anything (problems will start if you try to use the same variable twice without doing something with the old value)
So here is a good example of DIMing with and issue

'----------------------------------------------------------------------
' Yeshai Bouskila - Some Script
'----------------------------------------------------------------------    
  	set objShell = CreateObject("Wscript.Shell")
  	set objFSO = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------------------------
	' Some Variables for Date, Year TOD
	D = Day(Date)
	Y = Year(Date)-2000
	T = Hour(Time) & Minute(Time)
	WScript.Echo(Y)
	WScript.Echo(T)
	
	Y = "Yeshai"
	T = "What???"
	WScript.Echo(Y)
	WScript.Echo(T)

So as you can see above I’ve used my Y and T variable’s twice…. first time they had my Year and Time value and second time they got the values I assigned to them, Yeshai and What???
Here is my quick test output screen

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
11
2255
Yeshai
What???
***** script completed – exit code: 0 *****

Now in a real world you would write it like so:

	Option Explicit
'----------------------------------------------------------------------
' Yeshai Bouskila - Some Script
'----------------------------------------------------------------------    
  	set objShell = CreateObject("Wscript.Shell")
  	set objFSO = CreateObject("Scripting.FileSystemObject")
'----------------------------------------------------------------------
	' Some Variables for Date, Year TOD
	Dim D, Y, T
	D = Day(Date)
	Y = Year(Date)-2000
	T = Hour(Time) & Minute(Time)
	WScript.Echo(Y)
	WScript.Echo(T)
	
	Dim Y,T
	Y = "Yeshai"
	T = "What???"
	WScript.Echo(Y)
	WScript.Echo(T)

And the error would be “Testing.vbs(19, 6) Microsoft VBScript compilation error: Name redefined” (From my output box of VBS Edit) but you would get the same error if you run the file.

So bottom line here do what ever you want to do, one persons best practice is not always the right way, an you don’t have to listen to me as i’m not a real developer but do this both for fun and work….

This entry was posted in Visual Basic and tagged , , , , , . Bookmark the permalink.
  • octet

    Lies!