Visual Basic Data Types

In Visual Basic, Data Types are useful to define a type of data the variable can hold such as integer, float, string, etc. in our application.

 

Visual Basic is a Strongly Typed programming language so before we perform any operation on a variable, it’s mandatory to define a variable with a required data type to indicate what type of data the variable can hold in our application.

Syntax of Defining Visual Basic Data Types

Following is the syntax of defining data types in visual basic.

 

Dim [Variable Name] As [Data Type]

Dim [Variable Name] As [Data Type] = [Value]

If you observe the above syntax, we added a required data type after the variable name to tell the compiler about the type of data the variable can hold.

 

ItemDescription
Dim It is useful to declare and allocate the storage space for one or more variables.
[Variable Name] It’s a name of the variable to hold the values in our application.
As The As clause in the declaration statement allows you to define the data type.
[Data Type] t’s a type of data the variable can hold such as integer, string, decimal, etc.
[Value] Assigning a required value to the variable.

Data Types in Visual Basic

Following table shows the list of available data types in visual basic programming language with memory size and range of values.

 

Data TypeSizeRange
Boolean Depends on Platform. True or False
Byte 1 byte 0 to 255
Char 2 bytes 0 to 65535
Date 8 bytes 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999
Decimal 16 bytes (+ or -)1.0 x 10e-28 to 7.9 x 10e28
Double 8 bytes -1.79769313486232e308 to 1.79769313486232e308
Integer 4 bytes -2,147,483,648 to 2,147,483,647
Long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Object 4 bytes on 32-bit platform, 8 bytes on 64-bit platform Any type can be stored in a variable of type Object
SByte 1 byte -128 to 127
Short 2 bytes -32,768 to 32,767
Single 4 bytes -3.4028235E+38 through -1.401298E-45 † for negative values; 1.401298E-45 through 3.4028235E+38 † for positive values
String Depends on Platform 0 to approximately 2 billion Unicode characters
UInteger 4 bytes 0 to 4,294,967,295
ULong 8 bytes 0 to 18,446,744,073,709,551,615 (1.8...E+19 †)
UShort 2 bytes 0 to 65,535
User-Defined Depends on Platform Each member of the structure has a range determined by its data type and independent of the ranges of the other members

Now, we will see how to use the Data Types in our visual basic applications with examples.

Visual Basic Data Types Example

Following is the example of using the data types in visual basic.

 

Module Module1

    Sub Main()

        Dim id As Integer

        Dim name As String = "Suresh Dasari"

        Dim percentage As Double = 10.23

        Dim gender As Char = "M"c

        Dim isVerified As Boolean

        id = 10

        isVerified = True

        Console.WriteLine("Id:{0}", id)

        Console.WriteLine("Name:{0}", name)

        Console.WriteLine("Percentage:{0}", percentage)

        Console.WriteLine("Gender:{0}", gender)

        Console.WriteLine("Verfied:{0}", isVerified)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined multiple variables with different data types and assigned the values based on our requirements.

 

When we execute the above example, we will get the result like as shown below.

 

Id:10

Name:Suresh Dasari

Percentage:10.23

Gender:M

Verfied:True

This is how we can use the data types in Visual Basic applications based on our requirements.

Topics Covered