VB.Net - Syntax
CType
This is a type of explicit conversion
If this conversion is not possible then an exception is thrown.
When you use Option Strict On any data type conversions must be explicit.
Dim iNumber As Integer = CType(12.5, Integer)
Dim iNumber As Integer = CType("12.5", Integer)
The second argument is the name of the target class and is not enclosed in quotes.
Can be used on any valid expression.
If the argument cannot be cast directly to the target type in the case of the above conversion from a double to an integer.
CType will attempt to convert it even if it means losing the fractional part of the number.
If the argument cannot be converted to the target type in the case of a string to a number.
Dim iNumber As Integer = CType("sometext", Integer)
Then the CType operator will throw an InvalidCastException error.
This is compiled inline, which means that the conversion code is part of the evaluation.
In somes cases there is no call to something else for evaluation, which makes execution faster.
If a narrowing conversion fails an OverflowException error is generated.
The following line will not compile
Dim myInteger As Integer
myInteger = myDouble
You must use
myInteger = CType(myDouble, Integer)
Never declare a new object when it is declared, (eg Dim object as New Connection). Always declare the object and then set the object before you actually need it and always remember to set it to NOTHING (eg set object = Nothing) when you have finished with it.
When using the IsMissing function the variable must always be declared as variant.
If you have any problems with the Int() or Cint() type conversion functions then use the Str() function to return a string representation of the number first, before converting.
This is the same as using (Brackets)
This function is not available in VBA.
DirectCast
This is a type of explicit conversion
If this conversion is not possible then an exception is thrown.
This is another way to convert between different data types.
The DirectCast works in a similar way to the CType operator but has a few imporatant differences:
It only works with arguments that are reference type. If you pass a Value type then a compilation error will occur.
It cannot perform widening conversions from Short to Integer.
It cannot perform widening conversions from Single to Double.
This method is slightly faster that CType so you should use CType when you need to cast rather than convert a value.
DirectCast requires an inheritance or implementation relationship between the data types of the two arguments.
This means that one type must inherit from or implement the other.
This is a cast operator that has the same syntax as CType and is useful in certain narrowing situations
The DirectCast gives you the CLR supported conversions rather than using any of the other conversion functions.
DirectCast(objObject, Integer) will generate an exception if objObject doesn't contain an integer.
CType(objObject, Integer) will work if objObject is a Long, Double, String etc
Using DirectCast instead of CType can make the conversion that little bit faster and should only be used when you know exactly what you are converting.
Dim arArray As New ArrayList()
Dim i as Integer
arArray.Add(100)
i = DirectCast(arArray(0), Integer)
DirectCast is twice as fast for value types (integers, etc) and marginally faster for reference types.
There is no boxing in DirectCast
DirectCast only works if the specified type and the run-time type of the expression are the same.
If the argument cannot be converted to the target type it will throw an InvalidCastException error.
This is the same as using (Brackets)
This operator is not available in VBA.
TryCast
This is a type of explicit conversion
If this conversion is not possible a Nothing value is returned.
This is a type of conversion operator that does not throw an exception.
Dim myText As String
myText = TryCast(myNumber, String)
If you try to do a coversion with DirectCast or CType and it fails an InvalidCastException is thrown.
Instead of generating an exception this operator return Nothing
This operator can only be used with reference types
C# ??
This operator is not available in VBA.
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext