This chapter introduced the fundamental types that all programs make use of: numeric types, string types, Boolean types, and date/time types. It also discussed how to form literals of the fundamental types and how to convert a value from one type to another. The topics for the next chapter are more complex data types that are formed using the fundamental types: arrays and enumerations. Here are some style points to consider. -
The ranges of the Integer and Long types have been widened from previous versions, and a new type, Short , has been added. The range of Short is the same as the range of Integer in previous versions, and the range of Integer is equivalent to the range of Long in previous versions. When you are converting code from previous versions of Visual Basic, it is important to keep in mind this difference. -
The characters % and & can be used as type characters for Integer and Long , respectively, but their inclusion is for historical reasons, and their use is discouraged. -
The Double type is the more natural floating-point size on the .NET Framework ”the Single type should not be used unless explicitly needed. -
In previous versions of Visual Basic, the runtime environment would throw an exception if the result of a floating-point operation was NaN. This is no longer the case, so Visual Basic .NET programmers must always check the result of floating-point division to ensure that it is not NaN. The functions Double.IsNaN and Single.IsNaN can be used for this purpose. -
The characters ! and # can be used as type characters for Single and Double , respectively, but their inclusion is for historical reasons, and their use is discouraged. -
Previous versions of Visual Basic supported both a Decimal and a Currency data type. The Currency data type also had a fixed precision but was only eight bytes. Because it had a smaller range than Decimal , it was dropped in Visual Basic .NET in favor of Decimal . -
The character @ can be used as a type character for Decimal (in previous versions it was used for the Currency type), but its inclusion is for historical reasons, and its use is discouraged. -
In general, programmers are encouraged to use strong typing ”that is, to specify the exact type of all the variables declared in the program. Strong typing ensures that many logic errors are caught at compile time; it also produces faster and more efficient code. The Option Strict statement ensures that you use strong typing in your code. -
Although they may appear to be functions, the conversion operators are an intrinsic part of the language. Because of that, using the conversion operators is much more efficient than calling the type conversion functions provided by the .NET Framework. The conversion operators should always be used unless there is a compelling reason not to do so. -
Because it requires narrowing conversions to be stated explicitly, using Option Strict is highly recommended as good programming practice. |