Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting DataTypes with DirectCast, CType, TryCast

Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using CType to do casting from one data type to another. I do this because it is simply faster to type, used to exist in VB6 and I do not know why I have to be using DirectCast if there is apparently no difference between them.

I use TryCast once in a while because I understand that sometimes casting can fail. I however cannot get the difference between CType and DirectCast.

Can anyone tell me the difference in plain simple English what the difference the two (CType and DirectCast)? Adding examples of where to use what as well would be helpful.

like image 727
Alex Essilfie Avatar asked Apr 24 '10 07:04

Alex Essilfie


People also ask

What is the difference between DirectCast and CType?

CType and DirectCast take an expression to be converted as the first argument, and the type to convert it to as the second argument. CType Function returns the result of explicitly converting an expression to a specific data type, object, structure, class, or interface.

What is TryCast?

TryCast returns Nothing, so that instead of having to handle a possible exception, you need only test the returned result against Nothing . You use the TryCast keyword the same way you use the CType Function and the DirectCast Operator keyword.

What is DirectCast in VB net?

You use the DirectCast keyword similar to the way you use the CType Function and the TryCast Operator keyword. You supply an expression as the first argument and a type to convert it to as the second argument. DirectCast requires an inheritance or implementation relationship between the data types of the two arguments.


2 Answers

DirectCast is twice as fast for value types (integers...etc), but identical for reference types.

For more information see the "Conversion Functions, CType, DirectCast, and System.Convert" section on this MSDN page.

like image 41
Oded Avatar answered Sep 21 '22 11:09

Oded


TryCast and DirectCast are casting operators that directly map to the CLR's support for casting. They can quickly cast an object of a base type to a derived type or unbox a value of a value type. DirectCast throws an exception when the cast isn't possible, TryCast returns Nothing if it failed. You typically want to favor DirectCast to catch programming mistakes.

CType allows a superset of conversions, ones that the CLR frowns on. The best example I can think of is converting a string to a number or date. For example:

Dim obj As Object obj = "4/1/2010" Dim dt As DateTime = CType(obj, DateTime) 

Which you'll have to use if Option Strict On is in effect. If it is Off then you can do it directly:

Option Strict Off ...     Dim dt As DateTime = obj 

Very convenient of course and part of VB.NET's legacy as a dynamically typed language. But not without problems, that date is Unicorn day at stackoverflow.com but will be a day in January when a Briton enters the string. Unexpected conversions is the reason the CLR doesn't permit these directly. The explicit, never a surprise conversion looks like this:

Dim dt As DateTime = DateTime.Parse(obj.ToString(), _     System.Globalization.CultureInfo.GetCultureInfo("en-US").DateTimeFormat) 

Whether you should buy into Try/DirectCast vs CType vs explicit conversions is rather a personal choice. If you now program with Option Strict On then you should definitely start using Try/DirectCast. If you favor the VB.NET language because you like the convenience of dynamic typing then don't hesitate to stay on CType.

like image 56
Hans Passant Avatar answered Sep 20 '22 11:09

Hans Passant