How to check wether CType() will throw InvalidCastException?

J

Joe HM

Hello -

I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastException? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDummy, String) otherwise I will just ignore it.

I could put a Try/Catch As System.InvalidCastException around it but I
was wondering if that is the best solution. I once heard that
exceptions should not be used as a tool but rather as a means to
intercept problems during run-time.

Thanks,
Joe
 
T

Tom Shelton

Hello -

I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastException? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDummy, String) otherwise I will just ignore it.

I could put a Try/Catch As System.InvalidCastException around it but I
was wondering if that is the best solution. I once heard that
exceptions should not be used as a tool but rather as a means to
intercept problems during run-time.

Thanks,
Joe

Dim o As SomeObject = TryCast(obj, SomeObject)
If Not o Is Nothing Then
' Do Cool Stuff
End If
 
J

Joe HM

Dim o As SomeObject = TryCast(obj, SomeObject)
If Not o Is Nothing Then
        ' Do Cool Stuff
End If

Hello -

Sorry ... I did not specify that I am using .NET 2003 and I just
checked but there (unfortunately) is no TryCast.

Thanks,
Joe
 
K

kimiraikkonen

Hello -

Sorry ... I did not specify that I am using .NET 2003 and I just
checked but there (unfortunately) is no TryCast.

Thanks,
Joe- Hide quoted text -

- Show quoted text -

Hi,
As you said and as you have v1.1 (i suppose), you can use a Try-Catch
block and leave Catch block empty. As you know, doing this may be
harmful in future because you won't be noticed if exception fails in
Catch block.

So, in addition to Tom,

'------------------------
Try

Dim o As SomeObject = TryCast(obj, SomeObject)
'Further processing here whether
'casting is successful

Catch
'Leave empty intentionally

End Try
'----------------------

HTH,

Onur Guzel
 
K

kimiraikkonen

Hi,
As you said and as you have v1.1 (i suppose), you can use a Try-Catch
block and leave Catch block empty. As you know, doing this may be
harmful in future because you won't be noticed if exception fails in
Catch block.

So, in addition to Tom,

'------------------------
Try

Dim o As SomeObject = TryCast(obj, SomeObject)
'Further processing here whether
'casting is successful

Catch
'Leave empty intentionally

End Try
'----------------------

HTH,

Onur Guzel- Hide quoted text -

- Show quoted text -

Opps, correcting, of course change TryCast with Ctype or Directcast as
follows:

'------------------------
Try


Dim o As SomeObject = DirectCast(obj, SomeObject)
'Further processing here whether
'casting is successful


Catch
'Leave empty intentionally


End Try
'----------------------


HTH,


Onur Guzel
 
C

Cor Ligthert[MVP]

Joe HM

Almost the code as Tom wrote for Net 1.1

\\\
Try
CType(obj, SomeType)
' Do Cool Stuff
Catch
'decide yourself if you want to handle this, I would do it for sure,
while Tom made it just simple as example in my idea.
End Catch
///

Cor



Dim o As SomeObject = TryCast(obj, SomeObject)
If Not o Is Nothing Then
' Do Cool Stuff
End If

Hello -

Sorry ... I did not specify that I am using .NET 2003 and I just
checked but there (unfortunately) is no TryCast.

Thanks,
Joe
 
J

Joe HM

Joe HM

Almost the code as Tom wrote for Net 1.1

\\\
Try
    CType(obj, SomeType)
   ' Do Cool Stuff
Catch
    'decide yourself if you want to handle this, I would do it for sure,
while Tom made it just simple as example in my idea.
End Catch
///

Cor




Hello -

Sorry ... I did not specify that I am using .NET 2003 and I just
checked but there (unfortunately) is no TryCast.

Thanks,
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks a lot for all the feedback. I ended up doing it as follows:
Try
lStringDummy = CType(lObjectDummy, String)
Catch lException As System.InvalidCastException
lStringDummy = "CType() FAILED"
End Try

That way I will not intercept any other exceptions.

I was thinking about doing what Patrice suggested (If TypeOf obj Is
String Then ...) but I also want to cast types other than Strings
(e.g. Integers). I guess I could have Or'ed something together but
using the Try/Catch seems to be more flexible.

Thanks again!
Joe
 
C

Cor Ligthert[MVP]

Joe,

I was only looking at the solution from Tom.

As ToString is a method of Object, and therefore inherited by every class
that exist. The most easiest way to convert any object to a string is.

Whatever.toString

Cor

"Joe HM" <[email protected]> schreef in bericht
Joe HM

Almost the code as Tom wrote for Net 1.1

\\\
Try
CType(obj, SomeType)
' Do Cool Stuff
Catch
'decide yourself if you want to handle this, I would do it for sure,
while Tom made it just simple as example in my idea.
End Catch
///

Cor




Hello -

Sorry ... I did not specify that I am using .NET 2003 and I just
checked but there (unfortunately) is no TryCast.

Thanks,
Joe- Hide quoted text -

- Show quoted text -

Hello -

Thanks a lot for all the feedback. I ended up doing it as follows:
Try
lStringDummy = CType(lObjectDummy, String)
Catch lException As System.InvalidCastException
lStringDummy = "CType() FAILED"
End Try

That way I will not intercept any other exceptions.

I was thinking about doing what Patrice suggested (If TypeOf obj Is
String Then ...) but I also want to cast types other than Strings
(e.g. Integers). I guess I could have Or'ed something together but
using the Try/Catch seems to be more flexible.

Thanks again!
Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top