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). The means to name a value, store that value and recall it 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 are in fact a lot of differing datatypes available to you as a Visual Basic programmer. They differ in the amount of memory they require, the range of values you can store in them and the types of value you can store in them.
Now, numbers and text form a large part of the data types available but there are datatypes for True/False values (Boolean), Dates, whole numbers (integers) and decimalised (floating point) numbers. The following table summarises the main datatypes available to you as a VB.NET programmer. Note, there are more datatypes available in .NET but this table illustrates the main protagonists.
|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 ie Visual Basic is slower at performing calculations with these variables than any other datatype.|
|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 datatypes. 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 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 this is the same unfortunately; this tutorial is the equivalent of memorising the legal limit of alcohol in your bloodstream.
It is possible to convert variables from one datatype 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 typcally 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)