Variables

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

 

TEMPORARY STORAGE:

Most programs require the temporary storage of data. The data is stored in a variable, which is a temporary storage in the computer's memory. The variable name is the "identifier", used to identify the data. Variables names must begin with a letter and comprise a series of alphanumeric and '_' up to 255 characters in length. Visual Basic 6 does not require that a variable be declared before use, but this is a dangerous practice and can lead to logical errors that are difficult to detect. In order to force variable declaration, specify Option Explicit in General Declarations. VB.Net does require variables to be formally declared.

When a variable name is first used, Visual Basic automatically creates it and assigns a default value of zero for numeric data types, null string for a String variable, False for Boolean, and Empty for a variant.

SCOPE AND DURATION:

The scope and duration of a variable is determined by the keywords Dim, Redim, Static, Public or Private. Whilst declarations can appear anywhere within the body of code, it is good programming practice to declare all variables at the beginning.

SCOPE OF VARIABLE:

 

Declaration Type Scope and Duration
Dim
Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are reset (eg. an Integer data type will be reset to zero).
Static
Used to declare variables in procedures. Each time the procedure is called, the data stored in variables declared with this keyword are retained.
Public
Used to declare variables within a module. The variable is available to all procedures in the project.
Private
Used to declare variables within a module. The variable is only available to procedures within the project.
ReDim
The ReDim keyword is used to re-dimension an array.

 

DATA TYPES OF VARIABLES:

The data type of a variable is determined by the "As" keyword. If the As keyword is omitted, the data type is a "Variant". The following illustrates the basic data types that may be used.

DATA TYPE:

 

Data Type Example Description
Byte
Dim tinyNumber As Byte
Stores an integer in the range 0 to 255 (8 bits).
Boolean
Dim finished As Boolean
Stores a value that is either True or False.
Integer
Dim counter As Integer
Stores an integer (whole number) in the range -32768 to + 32767 (16 bits).

In VB.Net, the Integer data type is 32 bits. A new data type, "Short", has been introduced to represent 16-bit numbers.
Long
Dim longNumber As Integer
Stores an integer in the range -2,147,483,648 to +2,147,483,647 (32 bits).

In VB.Net, the Long data type is 64 bits bits.
Currency
Dim cost As Currency
Stores a real number in the range -922,337,203,685,477.5808 to +922,337,203,685,477.5807.
Single
Dim gradient As Single
Stores a single-precision floating point number, accurate to 8 significant digits (32 bits).
Double
Dim atoms As Double
Stores a double-precision floating point number, accurate to 16 significant digits (64 bits).
Date
Dim today As Date
Stores a Date.
String
Dim strSurname As String
Stores a sequence of up to 63,000 characters.
Variant
Dim unkown As Variant
The data type will change depending on what value is assigned to the variable. If not data type is specified, this is the data type that will be used.

In VB.Net, the Variant data type is no longer supported, and should be replaced by the "Object" data type.

 

DECLARING VARIABLES:

If no data type is specified when declaring a variable, its default type will be a Variant. If more that one variable is declared on the same line, the data type must be provided for each variable.

In the following example, x has no data type so will be declared as a Variant.

Dim x, y As Integer

 

The following declares the variables x and y as Integers.

Dim x As Integer, y As Integer

CONSTANTS:

A Constant value never changes. Visual Basic has some built in constants, such as, vbCrLf, to insert a carriage return / line feed on an output stream. Constants are declared with the Const keyword.

Const deadline As Date = #1/20/2002#

Once the constant has been assigned a value, the value can't be changed. Attempting to re-assign a value will result in a run-time error.

VISUAL BASIC CONVERSION FUNCTIONS:

Conversion functions may be used to convert data types to another type.

CONVERSATION FUNCTIONS:

 

Function Example Description
CBool
newValue = CBool(oldValue) Convert a data type to Boolean.
CByte
newValue = CByte(oldValue) Convert a data type to Byte.
CCur
newValue = CCur(oldValue) Convert a data type to Currency.
CDate
newValue = CDate(oldValue) Convert a data type to Date.
CDbl
newValue = CDbl(oldValue) Convert a data type to Double.
CInt
newValue = CInt(oldValue) Convert a data type to Integer.
CSng
newValue = CSng(oldValue) Convert a data type to Single.
CStr
strNewValue = CStr(oldValue) Convert a data type to String.
 

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641