Getting to Know Data Types

Getting to Know Data Types

As you know, a variable is really the name of a memory location that is used to store data. When you dimension a variable, you give it a name and a data type. The variable's data type determines how the data is stored in the computer's memory.

Many classic Visual Basic programmers have had limited exposure to data types. These programmers occasionally use variables that are not dimensioned, and other times they use Variant variables to hold string, integer, or Boolean values. Earlier versions of Visual Basic allowed you to play fast and loose with data types in this manner. If you didn't dimension a variable, Visual Basic would give the variable the default data type—a 16-byte Variant.

Although the Variant type has valid uses such as holding a reference to an Excel spreadsheet object, the Variant type is usually the last refuge of a sloppy programmer. I have seen some mighty sloppy Visual Basic code in which every variable is a Variant. This lack of precision not only wastes memory, it also is dangerous. For example, you can assign a Double value to a Variant and then inadvertently assign a string to the same variable. Previous Visual Basic compilers won't even blink at this blatant mismatch of data types.

Visual Basic .NET is far stricter about how you use variables. The value you assign to a variable must be assignment compatible with the variable's type. If you declare a variable to be an Integer, you can't automatically assign a Short value to it. Visual Basic .NET also disallows type coercion. Those of you who like to write code similar to myString = "The number is " + 10 will really need to pay attention.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net