Home

Variables and Data Types

 

Variable Declaration With Type

 

Introduction

As we will see in the next lessons, when a user is asked to provide a value in a program, the value is primarily considered a normal group of characters, also called a string. If you intend to involve that value in an operation that is not meant for a string, you should (usually must) first convert that string to the appropriate value. Fortunately, each data type that we are going to review presents an appropriate and easy mechanism to perform this conversion.

To specify the amount of memory that a variable would use,  on the right side of the variable's name, you type the As keyword followed by the data type. The formula to declare such a variable is:

Dim VariableName As DataType
 
 

Type Characters

To make variable declaration a little faster and even convenient, you can replace the As DataType expression with a special character that represent the intended data type. Such a character is called a type character and it depends on the data type you intend to apply to a variable. When used, the type character must be the last character of the name of the variable. We will see what characters are available and when it can be applied.

Numeric Data Types

 

Introduction

A natural number is one that contains either 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 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 languages and it is called the thousands separator. For US English, this character is the comma. The thousands separator symbol is used only to make the number easier to read. You will never use it in your code when referring to a number.

To support different scenarios, VBasic provides different types of natural numbers.

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, the number of fingers on an animal, etc.

To declare  a variable for a small number, use the Byte keyword. Here is an example:

Module Module1

    Sub Main()
        Dim StudentAge As Byte
    End Sub

End Module

A Byte variable is initialized with 0. Otherwise, to initialize it, you can assign it a small number from 0 to 255.

To convert a value to a Byte value, you can use CByte(). To do this, enter the value or the expression in the parentheses of CByte(). If the conversion is successful, CByte() produces a Byte value.

Short Integers

An integer is a natural number larger than the Byte. Examples of such ranges are the number of pages of a book, the age of a person. To declare a variable that can hold natural numbers in the range of -32768 to 32767, you can use the Short data type. 

Here is an example:

Module Module1

    Sub Main()
        Dim MusicTracks As Short
    End Sub

End Module

By default, a Short variable is initialized with 0. After declaring a Short variable, you can initialize it with the necessary value that must be a relatively small integer from -32768 to 32767. Here is an example:

Module Module1

    Sub Main()
        Dim MusicTracks As Short

        MusicTracks = 16
        MsgBox(MusicTracks)
    End Sub

End Module

In some cases, the compiler may allocate more memory than the variable needs, beyond the area required by a Short value. To indicate that the number must be treated as a Short and not another type of value, type s or S on the right of the initializing value. Here is an example:

Module Module1

    Sub Main()
        Dim MusicTracks As Short

        MusicTracks = 16S
        MsgBox(MusicTracks)
    End Sub

End Module

To convert a value to a short integer, you can use CShort() by entering the value or the expression in the parentheses of CShort(). If the conversion is successful, CShort() produces a Short value.

Integer

If you want a variable to hold values larger than the Short data type can accommodate, you can use the Integer data type to declare it. A variable declared with the Integer data type can hold a value between -2,147,483,648 and 2,147,483,647. Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfPages As Integer

    End Sub

End Module

Alternatively, you can use the % type character to declare an integral variable. Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfPages%

    End Sub

End Module

To convert a value to an integer, use CInt(): enter the value or the expression in the parentheses of CInt(). If the conversion is successful, CInt() produces an integral value.

After declaring an Integer variable, the compiler initializes it with a 0 value. Still, you can initialize it with the necessary value but you have three or four alternatives. To initialize the variable with a regular natural number, you can simply assign it that number. Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfPages As Integer

        NumberOfPages = 846
        MsgBox(NumberOfPages)
    End Sub

End Module

The VBasic language considers three types of integer values. Instead of just assigning a regular natural number, to indicate that the value must be considered as an integer, you can enter i or I on the right side of the value. Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfPages As Integer

        NumberOfPages = 846I
    End Sub

End Module

The second type of integral number is referred to as hexadecimal. On a surface, it is primarily a different way of representing a number by using a combination of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, A, B, C, D, E, or F. This still allows you to represent any natural number. To initialize an integer variable with a hexadecimal number, start the value with &h or &H. Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfStudents As Integer

        NumberOfStudents = &H4A26EE
    End Sub

End Module

Besides the regular natural or the hexadecimal numbers, the third type of value an Integer variable can hold is referred to as octal. This is a technique used to represent a natural number using a combination of 0, 1, 2, 3, 4, 5, 6, and 7. To use such a value, start it with &o or &O (the letter O and not the digit 0). Here is an example:

Module Module1

    Sub Main()
        Dim NumberOfStudents As Integer

        NumberOfStudents = &O4260
    End Sub

End Module

Long Integer

A long integer is a very large natural number. Examples are the population of a city, the distance between places of different countries, the number of words of a book.

To declare a variable that can hold a very large natural number, use the Long data type. Here is an example:

Module Module1

    Sub Main()
        Dim Population As Long

    End Sub

End Module

Instead of the AS Long expression, as an alternatively, you can also use the @ symbol as the type character to declare a Long variable. Here is an example:

Module Module1

    Sub Main()
        Dim Population@
        
    End Sub

End Module

To convert a value to a long integer, you can use CLng(). To do this, enter the value or the expression in the parentheses of CLng(). If the conversion is successful, CLng() produces a Long integer value.

Like all integers, a Long variable is initialized, by default, with 0. After declaring a Long variable, you can initialize with the necessary natural number. Here is an example:

Module Module1

    Sub Main()
        Dim Population@

        Population@ = 9793759
        MsgBox(Population)
    End Sub

End Module

To indicate that the value must be treated as Long, type l or L on the right side of the number. Here is an example:

Module Module1

    Sub Main()
        Dim Population@

        Population@ = 9793759L
  
    End Sub

End Module

Because Long is primarily an integral like the Integer data type we reviewed earlier, you can also initialize it using a decimal, a hexadecimal, or an octal value. Here is an example:

Module Module1

    Sub Main()
        Dim Population@

        Population = &HFF42AD
    End Sub

End Module
 
 

Previous Copyright © 2004-2007 FunctionX, Inc. Next