Representing Words, Numbers, and Values with Variables

Variables are an important concept in computer programming. A variable is a letter or name that can store a value. When you create computer programs, you can use variables to store numbers, such as the height of a building, or words, such as a person's name. Simply put, you can use variables to represent any kind of information your program needs.

You might ask, "Why use a variable when I could just use the information instead?" As the name implies, variables can change the value that they represent as the program is running. For example, you might write a program to track the number of pieces of candy you have in a jar on your desk. Because candy is meant to be eaten, the number of pieces of candy in the jar is likely to change over time. Rather than rewriting your program every time that you get a sugar craving, you can represent the number of pieces of candy with a variable that can change over time.

Storing Information in Variables

There are three steps to using a variable:

  1. Declare the variable. Tell the program the name and kind of variable you want to use.

  2. Assign the variable. Give the variable a value to hold.

  3. Use the variable. Retrieve the value held in the variable and use it in your program.

Declaring Variables

When you declare a variable, you have to decide what to name it and what data type to assign to it. You can name the variable anything that you want, as long as the name starts with a letter or an underscore. When you use a name that describes what the variable is holding, your code is easier to read. For example, a variable that tracks the number of pieces of candy in a jar could be named totalCandy.

You declare a variable using the Dim and As keywords, as shown here.

Dim aNumber As Integer

This line of code tells the program that you want to use a variable named aNumber, and that you want it to be a variable that stores whole numbers (the Integer data type).

Because aNumber is an Integer, it can store only whole numbers. If you had wanted to store 42.5, for example, you would have used the Double data type. And if you wanted to store a word, you'd use a data type called a String. One other data type worth mentioning at this point is Boolean, which can store a True or False value.

Here are more examples of how to declare variables.

Dim aDouble As Double
Dim aName As String
Dim YesOrNo As Boolean

Note

You can create a local variable without declaring the type of the variable by using local type inference. When you use local type inference, the type of the variable is determined by the value that is assigned to it. For more information, see Local Type Inference (Visual Basic).

For more information about other variable types, see Closer Look: Data Types.

Assigning Variables

You assign a value to your variable with the = sign, which is sometimes called the assignment operator, as shown in the following example.

aNumber = 42

This line of code takes the value 42 and stores it in the previously declared variable named aNumber.

Declaring and Assigning Variables with a Default Value

As shown earlier, you can declare a variable on one line of code, and then later assign the value on another line. This can cause an error if you try to use the variable before assigning it a value.

For that reason, it is a better idea to declare and assign variables on a single line. Even if you don't yet know what value the variable will hold, you can assign a default value. The code for declaring and assigning the same variables shown earlier would look like the following.

Dim aDouble As Double = 0
Dim aName As String = "default string"
Dim YesOrNo As Boolean = True

By declaring variables and assigning default values on a single line, you can prevent possible errors. You can still use assignment to give the variable a different value later.

Try It!

In this exercise, you will write a short program that creates four variables, assigns them values, and then displays each value in a window called a message box. Let's begin by creating the project where the code will be stored.

To create the project

  1. If it is not already open, open Visual Basic from the Windows Start menu.

  2. On the File menu, click New Project.

  3. In the New Project dialog box on the Templates pane, click Windows Forms Application.

  4. In the Name box, type Variables and then click OK.

    Visual Basic will create the files for your program and open the Form Designer.

Next, you'll create the variables.

To create variables and display their values

  1. Double-click the form to open the Code Editor.

    The Code Editor opens to a section of code called Form1_Load. This section of code is an event handler, which is also referred to as a procedure. The code that you write in this procedure is the instructions that will be performed when the form is first loaded into memory.

  2. In the Form1_Load procedure, type the following code.

    Dim anInteger As Integer = 42
    Dim aSingle As Single = 39.345677653
    Dim aString As String = "I like candy"
    Dim aBoolean As Boolean = True
    

    This code declares four variables and assigns their default values. The four variables are an Integer, a Single, a String, and a Boolean.

    Tip

    As you typed the code, you may have noticed that after you typed As, a list of words appeared underneath the cursor. This feature is called IntelliSense. It enables you to just type the first few letters of a word until the word is selected in the list. Once the word is selected, you can press the TAB key to finish the word.

    Note

    Whenever you represent actual text in a program, you must enclose it in quotation marks (""). This tells the program to interpret the text as actual text instead of as a variable name. When you assign a Boolean variable a value of True or False, you do not enclose the word in quotation marks, because True and False are Visual Basic keywords with special meanings of their own.

  3. Beneath the code you wrote in the previous step, type the following.

    MsgBox(anInteger)
    MsgBox(aSingle)
    MsgBox(aString)
    MsgBox(aBoolean)
    

    This code tells the program to display each value that you assigned in the previous step in a new window, using the MsgBox function.

  4. Press F5 to run your program.

    Click OK for each message box as it appears. Note that the value of each variable is displayed in turn. You can close the form by clicking the x in the upper-right corner of the form. After the program has finished, you can go back and change the values that are assigned in the code—you'll see that the new values are displayed the next time that you run the program.

Next Steps

In this lesson, you learned the basics about variables. In the next lesson, you will learn more about String variables.

Next Lesson: Words and Text: Using String Variables to Organize Words

See Also

Tasks

Words and Text: Using String Variables to Organize Words

Closer Look: Converting from One Variable Type to Another

Concepts

The Basics: How Programming Works

Closer Look: Data Types