In the previous tutorial I described the notion of variables and what they represent in Visual Basic (and any other high level programming language for that matter). Variables allow you to store values, give those values a name and recall the value in your application. The values could be both number and text and, if you recall, we used an Integer variable to store a whole number. There is, in fact, a number of differing data types available to you as a Visual Basic programmer. They differ in the amount of memory they require to store a value, the range of values you can store in them and the types of value you can store in them. Numbers and text are the predominant types of data available in Visual Basic, but there are data types for True/False values (Boolean), Dates, whole numbers (integers) and decimalised (floating point) numbers.
The following table summarises the main data types available to you as a VB.NET programmer. Note, there are more data types available in .NET but this table illustrates the main protagonists.
|Data type||Storage allocation||Values|
|Boolean||Depends on implementing platform||True or False|
|Byte||1 byte||Whole Numbers.0 through 255|
|Char||2 bytes||Whole Numbers.0 through 65535|
|Date||8 bytes||Dates and Times|
|Decimal||16 bytes||Huge Decimal numbers. Use with caution as the performance is relatively very poor i.e. Visual Basic is slower at performing calculations with these variables than any other data type.|
|Double||8 bytes||Decimal Numbers.|
|-1.79769313486231570E+308 through -4.94065645841246544E-324 † for negative values;|
|4.94065645841246544E-324 through 1.79769313486231570E+308 † for positive values|
|Integer||4 bytes||Whole Numbers.-2,147,483,648 through 2,147,483,647|
|Long||8 bytes||Whole Numbers-9,223,372,036,854,775,808 through 9,223,372,036,854,775,807|
|SByte||1 byte||Whole Numbers,-128 through 127|
|Short||2 bytes||Whole Numbers.-32,768 through 32,767|
|Single||4 bytes||Decimal Numbers.|
|-3.4028235E+38 through -1.401298E-45 for negative values|
|1.401298E-45 through 3.4028235E+38 for positive values|
|String||Depends on implementing platform||Text. Can handle 0 to approximately 2 billion Unicode characters|
I wouldn’t spend too much time at this stage learning these data types. It’s boring more than anything else! In the main you will use Integers for whole numbers, Double for decimal numbers, String for text and Boolean for true/false values. The rest are for more specialised cases where large or small numbers are desired or required. Coding is much more fun than this, I promise you. Can you remember learning to drive? Learning the gears and stopping distances was as fun as putting salt in your eyes, but careering round a track at 100mph is damn fun. Well programming is the same and this tutorial is the equivalent of memorising the legal limit of alcohol in your bloodstream when driving.
Before we hurry away from this tutorial, it’s worth noting that it is possible to convert variables from one data type to another. For example a String of “111” can be converted to an Integer and the value would be 111. You will get an error if you try to convert “sss” to an Integer though. The conversion syntax is typically e.g. variable.ToString or variable.ToInt.
The following video covers Data Types in Visual Basic.
Enjoy, if you can (I’m off to relearn braking distances in wet weather)