Computer programs are processors of data and therefore, they need of way of representing various types of data -- numbers, alphabetic characters, special characters, and the like -- inside the computer.
A D V E R T I S E M E N T
Also,during processing programs need a way to hold onto -- to store -- these data inside computer memory.
Variables are named storage areas inside computer memory where a program places data during processing. A program sets aside these storage areas by declaring variables, that is, by assigning them a name and indicating what types of data will occupy these areas during processing.
Within a Visual Basic program, variables are declared using the Dim statement whose general format is shown below.
Dim variable As type
where type is one of the data types permissible in Visual Basic and variable is a programmer-supplied name for the storage area.
Programmer-supplied variable names are also referred to as identifiers,
and they must conform to Visual Basic naming conventionsand a variable name must
Be composed only of alphabetic,underscore, and numeric characters.
Begin with an numeric or alphabetic character, or the underscore (_)
Not contain any embedded blank spaces.
Not be the same as a Visual Basic keyword, words the comprise of the
Visual Basic language itself.
Variable names are not case sensitive, so the name expressed in
upper-case characters refers to the same storage area as a name expressed in
lower-case characters and however, for programming consistency, and to avoid
unnecessary errors, it is best to express a variable as it is originally
declared. Thus, valid variable names include X,
My_Data_Variable, and other names using meaningful and simple
When declaring a variable an indication can be given about what type of data
will be stored and there are many data types permissible under Visual Basic.In computer
processing,the following are common for declaring the usual types of data involved:
Short. A non-decimal (whole) number
with a value in the range of the -32,768 to +32,767.
Integer. A non-decimal (whole) number
with a value in the range of the -2,147,483,648 to +2,147,483,647.
Long. A non-decimal (whole) number with
a value in the range of the -9,223,372,036,854,775,808 to
Single. A floating-point (decimal)
positive or negative number with a value in the range of up to the 3.402823E38
Double A floating-point (decimal)
positive or negative number with a value in the range of up to the
Decimal. A decimal number with a value
in the range of up to +79,228,162,514,264,337,593,950,335. without decimal
precision and up to the +7.9228162514264337593543950335 with 28 places of
String. Any number of special,
numerical,alphabetic and special characters.
Date Dates in the range between
January 1, 0001 to December 31, 9999 and times in the range between 0:00:00
to the 23:59:59.
Boolean. The value of True or False.
Variables, then, are declared by assigning a name and a data type and the
following are examples of valid variable declarations that can be used to store
data of the identified types.
Dim My_Counter As Integer
Dim My_Salary As Decimal
Dim My_Great-Big-Number As Double
Dim My_Name As String
Dim My_Birthday As Date
Dim My_Current_Time As Date
Dim My_Decision As Boolean
Assigning Values to Variables
With the Visual Basic assignment statement,variables are assigned values whose general format is shown below.
Variable declarations simply set aside memory storage areas as temporary containers of data -- temporary because they are no longer accessible once the program ends and during processing, of course, these variables are occupied by data values.
Data for processing can come from many different sources and they can come from outside the program -- from external files and databases; they can come from inside the program by explicitly setting a value for a variable or by storing processing results of manipulating other variables. In any case, variables take on values by assigning values to them.
variable = value
Assigning Expressions to Variables
Some type of arithmetic, string, or date manipulation operation takes place with the "answer" stored
in the variable.The most common scenario is that the results of processing are assigned to a variable.
Although these types of operations are discussed later, you should be able to understand the principles involved in the following assignment statements.
Dim The_Answer As Decimal
Dim Var_A As Decimal = 2.5
Dim Var_B As Decimal = 3.5
The_Answer = 5 * (Var_A + Var_B)
The Variant Data Type
If variable types are not explicitly declared -- using As Integer, As Decimal, As String, etc. -- then values assigned to the variable are stored as a Variant data type and any type of data can be stored in the variable and any type of operation can be performed on it. It is a general-purpose data type that provides flexibility in storing different types of data in a single variable. It can be assigned a numeric value that is immediately replaced by a string value.
Although it does offer some programming flexibility, there are minor inefficiences associated with converting different data types for representation in the same storage area and for this reason only it is best always to declare specific data types for specific variables. This practice of using strong typing of variables leads to more efficient programming and reduction in errors from inadvertently storing the wrong kinds of data in the wrong variables.
Values of one data type can be converted to another data type using type conversion. This operation is called casting from one type to another and casting requires calling a conversion function to switch a data type. The resulting cast must be assigned to a variable of the cast type.
Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
Variables and Data Types in VB.NET,
vb net data,