Any decent introductory book on Visual Basic would cover data types. Some books go in depth, whereas other books simply just give data types a mention. Not too long ago, I used to teach programming classes on a permanent basis; now, I only do it part time. Explaining a concept as simple as a Data Type can sometimes end up consuming a lot of time. I suppose for a newbie it is hard to envisage the whole concept of temporary information being stored depending on a data type. Whatever the case may be, today's article will help you fully understand Visual Basic and .NET data types.
MSDN defines a Visual Basic data type the following way:
The data type of a programming element refers to what kind of data it can hold and how it stores that data. Data types apply to all values that can be stored in computer memory or participate in the evaluation of an expression.
I define a Visual Basic data type the following way:
A data type tells the programming element what type of data it can hold, how much space in memory the programming element should take up, and what the programming element is allowed to do or not.
Table 1 indicates the allotted memory space for each Visual Basic data type as well as each data type's acceptable range of values:
|Visual Basic Data Type||Memory Storage Allocation||Allowable Value(s)|
|Boolean||Depends on implementing platform||True or False|
|Byte||1 byte||0 through 255|
|Char||2 bytes||Codepoints 0 through 65535|
|Date||8 bytes||0:00:00 AM on January 1, 0001 through 11:59:59 PM on December 31, 9999|
|Decimal||16 bytes||0 through +/-79,228,162,514,264,337,593,543,950,335 (+/-7.9...E+28) with no decimal point. 0 through +/-7.9228162514264337593543950335 with 28 places to the right of the decimal|
|Double (double-precision floating-point)||8 bytes||-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values|
|4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values|
|Integer||4 bytes||-2,147,483,648 through 2,147,483,647|
|Long (long integer)||8 bytes||-9,223,372,036,854,775,808 through 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 through 127|
|Short (short integer)||2 bytes||-32,768 through 32,767|
|Single (single-precision floating-point)||4 bytes||-3.4028235E+38 through -1.401298E-45 for negative values. 1.401298E-45 through 3.4028235E+38 for positive values|
|String(variable-length)||Depends on implementing platform||0 to approximately 2 billion Unicode characters|
|UInteger||4 bytes||0 through 4,294,967,295|
|ULong||8 bytes||0 through 18,446,744,073,709,551,615|
|UShort||2 bytes||0 through 65,535|
Table 1: Visual Basic Data Types
A few points worth noting with Table 1:
- The term Integer means a whole number
- Unsigned Integers can store only positive numbers
- Signed Integers can hold both positive and negative values
Reference & Value Types
Visual Basic data types are classified according to whether a variable can store its own data or a pointer to data. If a variable stores its own data, it is known a value type. If a variable stores a pointer to data elsewhere in memory, it is known as a reference type.
A reference type contains contains a pointer to another memory location that holds the data. The following are Reference types:
- All arrays
- Class types
More information on Reference types can be found on MSDN.
A value type holds the data within its own memory allocation. The following are Value types:
- All numeric data types
- Boolean, Date, and Char
- All structures
More information on Value types can be found here.
An array is a set of values that are logically related to each other.
The individual items of an array are called simply the elements of the array. Each item as an index that is contiguous from 0 through the highest index value. For more information regarding arrays, MSDN has a more detailed explanation.
An Enumeration is a list of named constants that, well, provides a list of options. These options could be a list of settings or whatever you decide it be, except for a list of strings that actually contain spaces. A list of other data types and structures can be found here.
A Visual Basic data type is one of the most fundamental building blocks of the Visual Basic language. If you can truly master proper Visual Basic data types, your application's performance would be better and the risk for errors a lot less.