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. 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.
Following is the syntax of defining data types in visual basic.
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.
Item | Description |
---|---|
Dim | It is useful to declare and allocate the storage space for one or more variables. |
[Variable Name] | It’s the variable's name 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. |
The following table shows the available data types in a visual basic programming language with memory size and range of values.
Data Type | Size | Range |
---|---|---|
Boolean | It depends on the 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 a 32-bit platform, 8 bytes on a 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 is 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.
Following is the example of using the data types in visual basic.
The above example shows that 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 as shown below.
This is how we can use the data types in Visual Basic applications based on our requirements.