VBScript: Multidimensional Arrays, Statements, and Commenting

We left off discussing variables and arrays in our last tutorial. I hinted at the existence of the mighty multi-dimensional array, but ran out of time before I could discuss this beast. However now I have an entire article to fill, so prepare to be amazed…multidimensionally.

In VBScript you can create arrays with up to sixty dimensions. You’ll probably never need to, but you know there is some nerd out there trying to pick up chicks by bragging about how he once created a 60 dimensional array that held every ancillary character from the Phantom Menace. I mean there are those people that build log cabins out of toothpicks to ensure they are remembered after passing over to the dark abyss.

Its far easier to show you how to use multidimensional arrays than to explain them, and since I am a lazy writer, that is exactly what I am going to do:


<html>

<body>

<script type="text/vbscript">

Dim cow(3,2)

cow(0,0) = "Mister Moo "

cow(0,1) = "Moo McGoo "

cow(0,2) = "Bessie "

cow(0,3) = "Elmer"

</script>

</body>

</html>

In the above example we create a multidimensional array, specifically, a two-dimensional one. You will notice that cow(3,2) has two numbers separated by a comma. This is because multidimensional arrays work similar to a grid or a spreadsheet, with the first number representing a row and the second number representing a column. So for instance, in the above code, Bessie appears in row 0 of column 2. You perform actions on multidimensional arrays the same as you would any other array.

{mospagebreak title=Dynamic Arrays}

Dynamic arrays are arrays whose size can be altered. To do so, you first initialize the array with no elements, then use ReDim to change its size, like so:


<html>

<body>

<script type="text/vbscript">

Dim cow()

Redim cow(2)

cow(0)=”Moo”

cow(1)=”Moo Moo”

cow(2)=”Moo Moo Moo”

Redim Preserve cow(3)

cow(3)=”Moo Moo Moo Moo”

</script>

</body>

</html>

The above code initially creates a dynamic array named cow, with no elements. We then use ReDim to change the number of elements within cow to 3 (cow(2)). Then we add some values to the array. Finally, we decide to add even more elements to cow, and ReDim it again to hold 4 values. We add the fourth value to the array, making sure to use the Preserve keyword. Had we not used Preserve, then all of the data in our array would have been deleted when we Redimmed it.

{mospagebreak title=Conditional Statements}

Just like other programming languages, VBScript has Conditional Statements. In their simplest form, these work on the following basis: if this is true, do that. VBScript has three conditional statements, starting with the…

If Statement

As stated above, the If Statement says: if this is true, do that. A real life example would be: "If mom yells that my Star Wars Spaghetti-Os are finished cooking, leave my super cool basement nerd pad to eat them."

Here it is in some code:


<html>

<body>

<script type="text/vbscript">

Dim sitcom

sitcom = “ALF”

If sitcom = “Alf” Then

document.write(“Don’t eat my cat!”)

End If

</script>

</body>

</html>

The above code created a variable named sitcom and assigns it the value “Alf.” It then performs an If statement that says if the value of sitcom is Alf, then print out some text. If the value of sitcom had not been Alf, nothing would have happened. Since the criteria was met however, this would be the result:

  Don’t eat my cat!

That’s all fine and dandy if the criteria is met, but what if it wasn’t? What if we wanted to respond if the value of sitcom had not been Alf? For that we would use the Else Clause.

You Better or….Else

When people say that it’s never a good thing. No one ever says things like "You better clean your room or else I will do it for you while you eat these warm brownies I just baked." Instead its always stuff like "You better quit calling me, sending me notes, and hiding in the bushes outside my window with those binoculars or I will inform the police that you are not following the restraining order I had to put on you because you couldn’t handle that I left you for your cousin Tina at your seventeenth birthday party." Women…

Here is some code showing the Else clause at work:

<html>

<body>

<script type="text/vbscript">

Dim sitcom

sitcom = “Mister Belvedere”

If sitcom = “Alf” Then

document.write(“Don’t eat my cat!”)

Else

document.write(“Hey change the channel, Alf is on!”)

End If

</script>

</body>

</html>

The above code creates the variable sitcom and adds the value, “Mister Belvedere.” It then creates an If statement that asks if the value of sitcom is “Alf.” If it is, it will print: Don’t eat my cat! to the screen. If not, will print the following:

  Hey change the channel, Alf is on!

{mospagebreak title=If…Then…Elseif}

If you want to set multiple If then Elses, you would use the ElseIf statement. Behold!


<html>

<body>

<script type="text/vbscript">

Dim sitcom

sitcom = “Greatest American Hero”

if sitcom = “Alf” Then

msgbox “Don’t eat my cat!”

elseif sitcom = “Three’s Company”

msgbox “Larry said to meet him at the Blue Oyster”

elseif sitcom = “Alice”

msgbox “Kiss my grits!”

elseif sitcom = “Greatest American Hero”

msgbox “Prepare for a crash landing”

else

msgbox “Hey turn the tv, Alf is on!”

End If

</script>

</body>

</html>

Again, we create a variable named sitcom and assign it a value. Then we run through a series of statements, asking if the value of sitcom is equal to Alf; if it is, the program creates a pop-up box that says "Don’t eat my cat!" Otherwise it moves on to the next statement and asks, is the value equal to Three’s Company? If so, it creates a pop-up that says "Larry said to meet him at the Blue Oyster." It continues on in this manner until it finds a match. Finally at the end, if no match is found, it creates a pop-up box that says: "Hey turn the tv, Alf is on!"

However, since it does find a match, the program shows the following message in a pop-up box and exits out of the If statement:

  Prepare for a crash landing

{mospagebreak title=The Select Case}

As an alternative to the Elseif statement you could use the Select Case statement, which saves time and space in a program and is easier to read. Here is a sample program based off of our previous program:


<html>

<body>

<script type="text/vbscript">

Dim sitcom

sitcom = “Greatest American Hero”

Select Case sitcom

Case “Alf”

msgbox “Don’t eat my cat!”

Case “Three’s Company”

msgbox “Larry said to meet him at the Blue Oyster”

Case “Alice”

msgbox “Kiss my grits!”

Case “Greatest American Hero”

msgbox “Prepare for a crash landing”

Case Else

msgbox “Hey turn the tv, Alf is on!”

End Select

</script>

</body>

</html>

This code functions in the same manner as our previous example, only it is much more efficient. You will notice that at the end of the code I used a Case Else; this isn’t required, but it gives you the option of saying if none of the criteria is met, do this.

{mospagebreak title=Commenting Your Code}

Since we don’t have time to cover Loops in this article, I will instead discuss commenting. Comments are used in code to let yourself and other programmers know what a piece of code was meant to do. It helps save time when debugging and rewriting code and is a good practice.

VBScript only supports one type of commenting, and that is the single line comment. You comment on code using the single quote (‘), like so:


<html>

<body>

<script type="text/vbscript">

‘This code will print out “Hey nerd” to your monitor

‘Look! Another comment!

document.write(“Hey nerd”)

</script>

</body>

</html>

When the program comes across your single quote, it ignores everything remaining on that line. It’s as if everything else is too hideous for VBScript to look at. Ah well, at least VBScript doesn’t run away screaming.

Well folks we’ve come to my favorite and your least favorite part of the article: the end. Join me next time when I talk about Loops and Functions in VBScript.

Till then…

One thought on “VBScript: Multidimensional Arrays, Statements, and Commenting

  1. Thanks once more for dropping by to read my article. Here we discuss Arrays, statements like the If and Switch, and commenting in VBScript. If you have any comments, article suggestions, or just want to shoot the breeze or have an idea about how to do things differently, feel free to leave me a comment. And also, if you like my work, feel free to follow the links below to read more:

    http://www.devshed.com/cp/bio/
    http://www.devarticles.com/cp/
    http://www.aspfree.com/cp/bio/

[gp-comments width="770" linklove="off" ]