The Role of System Convert

To wrap up the topic of data type conversions, I'd like to point out the fact that the System namespace defines a class named Convert that can also be used to widen or narrow a data assignment:

Sub Main()

Dim myByte As Byte Dim myInt As Integer = 200 myByte = CByte(myInt) myByte = CType(myInt, Byte) myByte = Convert.ToByte(myInt) Console.WriteLine("Value of myByte: {0}", myByte) End Sub

One benefit of using System.Convert is that it provides a language-neutral manner to convert between data types. However, given that Visual Basic 2008 provides numerous built-in conversion functions (CBool, CByte, and the like), using the Convert type to do your data type conversions is usually nothing more than a matter of personal preference.

Was this article helpful?

0 0

Post a comment