Data types in VB.NET
In this article I will explain about one of the main topics in VB.NET i.e. data types. You will also learn about the value and reference types.
There are two types of data type in VB.NET
Primitive data types are further divided as:
Byte
SByte
Short
UShort
Integer
UInteger
Long
ULong
Single
Double
Decimal
Char
Boolean
Date
String
Object
Non-primitive data types are further divided as:
Class
Structure
Enum
Interface
Delegate
Array
The built-in value types
VB Keyword
|
Bytes
|
.NET type
|
Description
|
Byte
|
1
|
Byte
|
0-255
|
SByte
|
1
|
SByte
|
-128 to 127
|
Short
|
2
|
Int16
|
-32,768 to +32,767
|
UShort
|
2
|
UInt16
|
0 to 65535
|
Integer
|
4
|
Int32
|
-2,147,483,648 to + 2,147,483,647
|
UInteger
|
5
|
UInt32
|
0 to 4,294,967,295
|
Long
|
8
|
Int64
|
-9,223,372,036,854,775,808 to+9,223,372,036,854,775,807
|
ULong
|
8
|
UInt64
|
0 to +18,446,744,073,709,551,615
|
Single
|
4
|
Single
|
A non integer number with approximately 7 significant digits
|
Double
|
8
|
Double
|
A non integer number with approximately 14 significant digits
|
Decimal
|
16
|
Decimal
|
A non integer number with approximately 28 significant digits (integer and fraction) that can represent values up to 79,228 X 1024
|
Char
|
2
|
Char
|
A single Unicode character
|
Boolean
|
1
|
Boolean
|
A True or False value
|
Common .NET structures that define value types
Structure
|
VB Keyword
|
What the value type holds
|
Byte
|
Byte
|
An 8-bit unsigned integer
|
Int16
|
Short
|
An 16-bit signed integer
|
Int32
|
Integer
|
An 32-bit signed integer
|
Int64
|
Long
|
An 64-bit signed integer
|
Single
|
Single
|
A single-precision floating-point number
|
Double
|
Double
|
A double-precision floating-point number
|
Decimal
|
Decimal
|
A 96-bit decimal value
|
Boolean
|
Boolean
|
True or False value
|
Char
|
Character
|
A single character
|
Common .NET classes that define reference types
Class Name
|
VB Keyword
|
What the reference type holds
|
String
|
String
|
A reference to a String object
|
Object
|
Object
|
A reference to any type of object
|
Variable
Example:
Dim age As Integer = 23
Constant
Example:
Const pi As Double = 3.1415
In case of constant the declaration and value assigning has to be at the same time.
Snippet code in VB.NET on variable and constant
Module Module1
Sub Main()
Dim age As Integer = 34
Const pi As Double = 3.1415
Console.WriteLine("Age is: " & age)
Console.WriteLine("Pi value is: " & pi)
Console.ReadLine()
End Sub
End Module
In .NET Microsoft has divided data types in two parts:
- Value Type (Fixed in size)
- Reference Type (Not fixed in size)
In application context, value types are stored in stack but reference types are stored in managed heap.
Value Type
-
Value types are fixed in size.
-
Value types are made in system stack.
-
Actual values of data are stored in stack.
-
If you assign a value of a variable to another it will create two copies.
All primitive data type except string and object are example of value types.
Object is a super type. It can store any type and any size of data. Object is called super type because it helps in inheritance.
struct and enum are value type.
Note: Stack is an operation entity (LIFO) i.e. it is fixed in size.
Reference Type
-
Reference types are not fixed in size.
-
They are maintained in system managed heap but it also uses stack to store reference of heap.
-
Two primitive types (string and object) and non-primitive data types (class, interface & delegate) are examples of reference type.
CLR manages heap (large memory area). Heap address is accessed from stack. In reference type reference is used for processing using both managed heap and stack (operational entity).
Conclusion
Hope the article would have helped you in understanding data types in VB.NET.
Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments.