Absolutely, you want to know about the exception, but there may be a
situation where you want to handle the failure of type conversion by
checking whether the variable is Nothing or not. If the variable is not
nothing, then the type conversion succeeded. Here is the example.
[VB .NET]
Dim MyObject As MyType
Try
MyObject = CType(SomeObject, MyType)
Catch
MyObject = Nothing
End Try
If Not MyObject Is Nothing Then
' Do Some process
End If
[C#]
MyType MyObject = SomeObject as MyType;
if (MyObject != null)
{
' Do some process
}
VB .NET code is significantly more. And I assume that since it uses
exception to handle situation like it, performance cost may be higher in VB
..NET than in C#...
"Stephany Young" <st******@sysoft.co.nz> wrote in message
news:uh**************@TK2MSFTNGP11.phx.gbl...
I should hope not.
If I code a CType(...) and it fails then I damnned well want to know about
it.
"Hayato Iriumi" <hi*****@hotmail.com> wrote in message
news:u9**************@TK2MSFTNGP12.phx.gbl... When converting a type to another using CType and if the type conversion
fails, it throw an exception. However, in C#, there is a keyword "as"
which only makes the variable Nothing (null) without throwing an exception. Is
there a plan at Microsoft VB .NET team to include a keyword equivalent
to "as"?