VB.NET Type Conversion (Casting) Overview

VB.NET supports several type conversion functions. Widening conversions are handled by the compiler and don’t need to be explicitly written, but narrowing conversions require use of VB.NET’s type conversion functions.

The three type conversion functions are:

  • DirectCast
  • CType (and CInt, CStr,…Cxxx)
  • TryCast

The compiler may not report an error if you code a narrowing conversion by accident. This is because Microsoft set Option Strict to be OFF for VB.NET projects (compared with C# projects where it is on). I think this was done to make upgrades from VB 6 easier to do since in VB 6 there was no Option Strict statement.

In my opinion one should always change Option Strict to be ON. This is just good coding practice and leads to more readable code with fewer ‘mysterious’ errors. I see no excuse these days to keep Option Strict set to OFF, and even the upgrade from VB 6 explanation seems somewhat odd as a reason.

To set Option Strict to be on, open your Web.config file. In the XML under system.web -> compilation you will see the ‘strict’ element attribute. Make sure that this is set to ‘true’. You may have to adjust some code when you try to compile your project, but it is definitely worthwhile going through the exercise.

DirectCast:

Using this type conversion option has rather limited usefulness. In order not to generate an InvalidCastException error you need to know the type of the object. For example the DirectCast(myVariable, String) command will only work if myVariable is already a String.

CType  (and CInt, CStr,…Cxxx):

Possibly the most useful form of type conversion since this will convert types that don’t have an inheritance relationship (ie: String to int32). This conversion will return an InvalidCastException error if the data being cast does not match what is expected (ie: casting from String to int32 where the variable value is ‘abc’ will cause an error)

I am still undecided on whether to use the CType or the CInt syntax. The CInt, CStr, Cxxx syntax was brought over from VB 6 and continues to be supported, but the CType(myVariable, String) syntax just seems cleaner to me.

Interestingly CType is not the same as the C# casting operator, and can be used along the lines of the C# Convert.ToXx( syntax. For example the behavior of VB.NET’s CType(myVariable, Boolean returns a very different result from C#’s (bool)myVariable conversion. I experienced an interesting example of this when I wrote a component in C# and VB.NET to Telnet and communicate with a remote server.

TryCast:

This is a good way to try to cast from one type to another without generating an error. The common use is to check for Nothing as a result of TryCast at which point you know you have a problem.

The VB.NET  TryCast conversion is analogous to the C# ‘as’ operator functionality.

Matching the TypeOf operator with any of the type conversion functions can be useful in making sure that the data for the conversion is valid.

Performance:

In general DirectCast and TryCast perform quite well. However, CType (or CStr,…Cxxx)  performs on average three times more slowly. This is because so much more is happening behind the scenes with CType (or CStr,…Cxxx).

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s