Type Conversion Functions
Earlier in the chapter, I recommended that you use Option Strict On and Option Explicit On. These settings will let the compiler help you catch errors at compile-time because they preclude a lot of implicit type conversions. You still need a good way to convert data from one type to another. To this end Visual Basic .NET includes dozens of type conversion capabilities.
Also noted throughout this chapter is the introduction of the ToString method in the Object class. Because every class and structure is an Object, every type contains a ToString conversion method. Many types also contain a method that takes a string argument and converts it to a specific type. For example, in the DateTime type, the Shared Parse method converts a string to a DateTime. The two statements below demonstrate converting a string to a DateTime and a DateTime back to a string, respectively.
Dim ADate As DateTime = DateTime.Parse("12:00:00 AM") Dim AString As String = ADate.ToString()
Many of the VB6 conversions have been carried over to VB .NET; a few have been replaced to support new types. In VB6, the conversion functions took an expression of a specific type or variant. In VB .NET the argument type is an Object, which means that you can pass any subclass of an Object that makes sense for the specific kind of conversion. Table 2.9 lists the VB6 conversion functions and their VB .NET replacements .
Table 2.9. VB6 Conversion Functions and Their VB .NET Replacements
As you can determine from Table 2.9, most conversion functions were carried over to VB .NET. Where the VB6 column contains None, no VB6 conversion function exists.
For reference, see Appendix A, "VB 6 Programming Element Changes in VB .NET."