VBScript – Hungarian Variables

Introduction to Hungarian Variables

Before we see the advantages of using the Hungarian Variable scheme, let us review what the term variable means in the context of VBScript.

What is a VBScript Variable?

A variable is the opposite of a constant.  Values of variables change, constants remain at their initial value.  Think of variables are placeholders for items that will change during the script.  By declaring and using variables in your VBScripts, you can manipulate their values later.  Technically the computer reserves a place in memory for each variable.

Counters would be a classic use of a variable.  If you wanted to create 10 computers and put them all in same OU, you could use a variable and then make the script loop ten times.  The result ten computers and an efficient script.

The Hungarian convention

The idea is to give extra information about the type of variable.   What the Hungarian convention does is to lay down a three letter prefix which instantly tells you what information to expect from the variable.  For example, if the variable represents text then it begins with str. Convention dictates that three letters are lowercase. 

  • int – Integer.  Example intComputerNum representing 10 or 50
  • str – String.  Example strOU or strDomain
  • obj – Object.  Example objUser or objComputer

 

There are other less common variables, for instance: err – error,  dtm – date, boo – Boolean.

A variation of the Hungarian Convention is to use a one letter prefix s for string, o for object.

Hungarian Convention Example:

A VBScript to create 10 Computer Accounts for machines called WKStation1 to WKStation10.

 

VBScript to demonstrate the Hungarian Convention
Guy Thomas January 2004

Dim objLeaf, objContainer, objRootDSE
Dim intComputerNum, strXPStation

Set objRootDSE = GetObject("LDAP://rootDSE")
Set objContainer = GetObject("LDAP://OU=Droitwich," & _
objRootDSE.Get("defaultNamingContext"))

For ComputerNum = 1 To 10
Set objLeaf = objContainer.Create("Computer", "cn=WKStation" & ComputerNum)
objLeaf.Put "sAMAccountName", "WKStation" & ComputerNum
objLeaf.SetInfo
Next
WScript.Echo "10 Workstations created."

Notes: The variable used here is ComputerNum  What our variable ComputerNum does is just add a number to the constant WKStation.  An inelegant alternative would be to have 5 lines dedicated to creating each computer account.  By using a variable, if you wanted 50 computers beginning with WKStation, it would be easy to amend the script. Change For ComputerNum = 1 to 10 –> For ComputerNum = 1 to 50.

Guy Recommends: Permissions Analyzer – Free Active Directory ToolFree Permissions Analyzer for Active Directory

I like thePermissions Monitor because it enables me to see quickly WHO has permissions to do WHAT.  When you launch this tool it analyzes a users effective NTFS permissions for a specific file or folder, takes into account network share access, then displays the results in a nifty desktop dashboard!

Think of all the frustration that this free utility saves when you are troubleshooting authorization problems for users access to a resource.  Give this permissions monitor a try – it’s free!

Download Permissions Analyser – Free Active Directory Tool

Naming variables

Choose meaningful names for your variable, a word or words that describe the purpose of the variable. Best practices suggests the length should be about 8 – 16 characters. 

Are variables case sensitive?  No. For instance, Computernum would be the same as ComputerNum or computernum.  That does not mean that you should not stick with the same capitalization throughout!

Another way of improving scripts is using ‘Option Explicit’ – See more here


See Also