Variables and Data Types

Variables and data types are integral part of all programming languages. Fundamentally all programs manipulate numbers and text, these are the building blocks of all programs. To clarify things let's look at how data is used in programming terms. Data is the stuff, the raw information that your program manipulates. Without data a program cannot perform any useful function. Programs manipulate data in many ways like adding and multiplying numbers or concatenating strings.

Data is stored in the memory of your computer while it is being processed, you can imagine this to the big wall full of boxes used in mail rooms to sort mails. You can put a letter in any box but unless the boxes are labeled with the destination address it's pretty meaningless.

Variables

are the labels on the boxes in your computer's memory. Variables are named data containers and each of which hold a single piece of information.

For example a variables can be used to store the user's name, or in a game situation, it can be used to save the player's score. Once you create a variable, you can recall the data stored inside it as many times as you wish. In addition, you can edit this data, update it, and at some situations, you might even want to delete the variable completely.



Let's look at variables and data types in more detail, essentially variables must have a name, a type and a value.

Variable Name

A variable names consist of any number of letters (an underscore counts as a letter) and digits. The first letter cannot be a number. In Visual Basic variable names are not case sensitive, so Name, name and NAme are all same variable names. Some examples of variable names:Number1, String1
Or
Number_1, String_1

Data Type or Variable Type

A variable in most cases has a data type which defines what type of data it can store. Let's look at some of the most important once.

Integer

Integers are whole numbers from a large negative value through to a large positive value. That's an important point to remember. Normally we don't think of numbers being restricted in size but on a computer there are upper and lower limits.

Double

A Double is a numeric data type which includes decimal numbers (i.e. fractions - meaning any number that is not a whole number).

String

These are literally any string or sequence of characters that can be printed on your screen.

Boolean

This type has only 2 values - either true or false. Boolean values are sometimes known as truth values because they are used to test whether something is true or not.

Variable Value

Once you have named a variable with its data type you can assign a value to it, here are some examples:My_Name="Sam"
My_Age=20
Had_Coffee=True
My_Score= 0.256
Now, let's put this all together. Variables and data types are fundamental to all programming languages. They are data items that represent a memory storage location in the computer. Variables are containers that hold data such as numbers and strings. Initialization of variables involve declaration with its type and assignment of a value to the variable. Here is a complete example:Declare My_Name as String
Declare My_Age as Integer
Declare Had_Coffee as Boolean
My_Name="Sam"
My_Age=20
Had_Coffee=True
That's about it for variables and data types. The next step is to learn how to manipulate data in variables, this is covered in Operators for Manipulating Data.

Back to Visual Basic for Beginners