Closer Look: Data Types
Data types in Visual Basic determine what kind of values or data can be stored in a variable, as well as how that data is stored. Why are there different data types? Think of it this way: if you had three variables, two of which held numbers while the third held a name, you could perform arithmetic using the first two, but you can't perform arithmetic on the name. Assigning a data type to a variable makes it easier to determine how the variable can—or can't—be used.
Data types are also used in other programming elements such as constants, properties, and functions. You will learn more about the other uses of data types in a following lesson.
Data Types for Numbers
Most computer programs deal with numbers in some form or another. Since there are several different ways to express numbers, Visual Basic has several numeric data types to deal with numbers more efficiently.
The numeric data type that you will use the most is the Integer, which is used to represent a whole number (a number without a fractional part). When choosing a data type to represent whole numbers, you will want to use the Long data type if your variable will be storing numbers larger than approximately two billion; otherwise an Integer is more efficient.
Not all numbers are whole numbers; for example, when you divide two whole numbers, the result is often a whole number plus a fraction (9 divided by 2 equals 4.5). The Double data type is used to represent numbers that have a fractional part.
There are additional numeric data types such as Decimal, Short, SByte, and UInteger; these are typically used in very large programs where memory usage or speed is an issue. For now, the basic numeric data types are all you will need. If you want to learn more about the advanced data types, see Numeric Data Types.
Data Types for Text
Most programs also deal with text, whether displaying information to the user or capturing text entered by the user. Text is usually stored in the String data type, which can contain a series of letters, numbers, spaces, and other characters. A String can be of any length, from a sentence or a paragraph to a single character to nothing at all (a null string).
For a variable that will always represent just one character, there is also a Char data type. If you only need to hold one character in a single variable, you can use the Char data type instead of a String.
Other Data Types
In addition to text and numbers, programs sometimes need to store other types of information, such as a true or false value, a date, or data that has a special meaning to the program.
For values that can be represented as true/false, yes/no, or on/off, Visual Basic has the Boolean data type. A Boolean variable can hold one of two possible values: True or False.
Although you can represent dates or times as numbers, the Date data type makes it easy to calculate dates or times, such as the number of days until your birthday or the number of minutes until lunch.
When you need to store more than one type of data in a single variable, you can use a composite data type. Composite data types include arrays, structures, and classes. You will learn more about these in later lessons.
Finally, there are some cases in which the type of data that you need to store may be different at different times. The Object data type allows you to declare a variable and then define its data type later. You will also learn more about the Object data type in a later lesson.
In the next lesson, "Words and Text: Using String Variables to Organize Words," you will learn more about working with string variables to form sentences.