Export (0) Print
Expand All
323 out of 424 rated this helpful - Rate this topic

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 jelly beans you have in a jar on your desk. Because candy is meant to be eaten, the number of jelly beans in the jar is likely to change over time. Rather than rewriting your program every time you get a sugar craving, you can represent the number of jelly beans with a variable that can change over time.

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.

When you declare a variable, you have to decide what to call it and what data type to assign to it.

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

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

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

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.

As shown above, you can declare a variable on one line of code, and then later assign the value on another line. This can result in 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 on.

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 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.

    The Code Editor opens to a section of code called Form1_Load. This section of code, which is called a procedure, contains instructions that will be carried out 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—an Integer, a Single, a String, and a Boolean—and assigns their default values.

    NoteTip

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

    NoteNote

    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)
    
    
    End
    
    

    The first four lines of code tell the program to display each value that you assigned in the previous step in a new window, using the MsgBox function. The final line tells the program to end after executing this procedure—it uses the End statement.

  4. Press F5 to run your program.

    Click OK for each window as it appears. Note that the value of each variable is displayed in turn, and then the program ends. After the program has finished, you can go back and change the values that are assigned in the code and run the application again—you'll see that the new values are displayed.

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

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.