VBS_2 – Variables

A variable can be used to store data.  The data can be numbers, text, or true/false values.  It is important to use meaningful names for variables.  In order to use a variable we must do two things:

  1. Declare the variable – is done by using “DIM” in-front of the variable name, which stands for “declare in memory”.  
  2. Assign a value to the variable – is done by using the equals symbol “=

Variable Types

  • An “integer” variable is a variable that stores a number that doesn’t have a decimal.  Examples: -5, 0, 7, 11, …
  • A “string” variable is a variable that stores text.  Examples: username, password, first name, ….
  • A “single” variable is a variable that stores a number that has a decimal place (also called “float”).  Examples: 2.5, 3.14, …

Variable Names
A common convention is to name variables base on what type of variable they are.  For example you might use “nameString” as your variable if it holds the name of your user.  Or “ageInteger” if it holds the age of the user.  Or “priceSingle for the price of something (as it has a decimal value).

Example Code:

DIM ageInteger
DIM nameString
DIM shoesizeSingle

Assigning values to Variables
There are two ways to give a variable a value:

  1. Assign a value to the variable in the code.  Example ageInteger = 17
  2. Assign a value to the variable that the user enters by using an input box.

Input Boxes – inputbox(“”)
The input box is a function much like the message box.  In fact it will display a message box, except it also has an empty text box for the user to input a response.  It also has a cancel button. After an input box is displayed the script waits for the user to enter the desired input.  After the user enters input the input box returns the values to the script.  It is important that input boxes be used in conjunction with a variable, otherwise you have no way to store their response!

Example code:

ageInteger = inputbox(“Enter your age: “)
nameString = inputbox(“Enter your name.”)
shoesizeSingle = inputbox(“Enter your shoe size.”)

Using and Displaying Variables
Once we have the input data from the user, we can use it in a calculation or display it back to the user.

Example code:

DIM nameString
nameString = inputbox(“What is your name?”)
msgbox “Nice to meet you ” & nameString & “.”

Notice a few things in the above code … First, I did not surround the ” ” around nameString variable.  This is because I wanted to display the value of the nameString variable not the name of the variable.  Second, I used the & symbol in-between each item to be displayed in the message box.  Third, the name of the variable “nameString” was chosen to communicate what the variable was for: name, and, what type of variable it was: string.



After working through the above examples…

Use Notepad++ or some such program and create a script that:

  • Declares a variable called colourString
  • Displays an input box that asks the user what their favourite colour is, and assigns it to colourString
  • Displays the message “Your favourite colour is: [the colour they entered].

Save your script file and run it to test it.  Then hand in your code as a .txt file.


Leave A Reply

Your email address will not be published. Required fields are marked *