Data types in Visual Basic

This is an article on Data types in Visual Basic in Visual Basic [VB].

Rated 4.00 By 1 users

When you decide to use a variable, you are in fact asking the computer to use a certain amount of space to hold that variable. Since different variables will be used for different purposes, you should specify the kind of variable you intend to use, then the computer will figure out how much space is needed for a particular variable. Each variable you use will utilize a certain amount of space in the computer's memory.

Before declaring or using a variable, first decide what kind of role that variable will play in your program. Different variables are meant for different situations. The kind of variable you want to use is referred to as a data type. To specify the kind of variable you want to use, you type the As keyword on the right side of the variable's name.

The formula to declare such a variable is:

Dim VariableName As DataType

Once you know what kind of variable you will need, choose the appropriate data type. Data types are organized in categories such as numbers, characters, or other objects.

String

A string is an empty text, a letter, a word or a group of words considered. To declare a string variable, use the String data type.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim City AsStringEndSub

After declaring the variable, you can initialize. If you want its area of memory to be empty, you can assign it two double-quotes.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim City AsString

City = ""EndSub

If you want to store something in the memory space allocated to the variable, assign it a word or group of words included between double-quotes.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim City AsString City = "Mumbai"EndSub

You can also initialize a string variable with another.

Boolean

A Boolean variable is one whose value can be only either True or False. To declare such a variable, use the Boolean keyword.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim Married AsBooleanEndSub

After declaring a Boolean variable, you can initialize by assigning it either True or False.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim Married AsBoolean Married = FalseEndSub

Like any other variable, after initializing the variable, it keeps its value until you change its value again.

Numeric Data Types

A natural number is one that contains only one digit or a combination of digits and no other character, except those added to make it easier to read. Examples of natural numbers are 122, 8, and 2864347. When a natural number is too long, such 3253754343, to make it easier to read, the thousands are separated by a special character. This character depends on the language or group of language and it is called the thousands separator. For US English, this character is the comma. The thousands separator symbol is mainly used only to make the number easier to read.

Byte

A byte is a small natural positive number that ranges from 0 to 255. A variable of byte type can be used to hold small values such as a person's age.

To declare a variable for a small number, use the Byte keyword.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim Student As ByteEndSub

Integer

An integer is a natural number larger than the Byte. It can hold a value between
-32,768 and 32,767.
To declare a variable of type integer, use the Integer keyword.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim Tracks AsIntegerEndSub

Long Integer

A long integer is a natural number whose value is between –2,147,483,648 and 2,147,483,642. To declare a variable that can hold a very large natural number, use the Long keyword.

Here is an example:

Code: VB

PrivateSub Form_Load()Dim Population AsLongEndSub

Decimal Data Types

A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.

Single

A single is a decimal number whose value can range from –3.402823e38 and –1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive. To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type.

Double

While the Single data type can allow large numbers, it offers less precision. For an even larger number, Microsoft Visual Basic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive. To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.

Here is an example of declaring a Double variable:

Code: VB

PrivateSub Form_Load()Dim Distance As DoubleEndSub

Currency

The Currency data type is used for a variable that can hold monetary values. To declare such a variable, use the Currency keyword.