473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to check wether CType() will throw InvalidCastException?

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
Nov 6 '08 #1
8 4931
On 2008-11-06, Joe HM <un*******@yahoo.comwrote:
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

--
Tom Shelton
Nov 6 '08 #2
On Nov 6, 11:37*am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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

--
Tom Shelton
Hello -

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

Thanks,
Joe
Nov 6 '08 #3
On Nov 6, 7:07*pm, Joe HM <unixve...@yahoo.comwrote:
On Nov 6, 11:37*am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:


On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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
--
Tom Shelton

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

Nov 6 '08 #4
On Nov 6, 8:21*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Nov 6, 7:07*pm, Joe HM <unixve...@yahoo.comwrote:


On Nov 6, 11:37*am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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
--
Tom Shelton
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- 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
Nov 6 '08 #5
Or you can use TypeOf :

If TypeOf obj Is String Then
etc...


"Joe HM" <un*******@yahoo.coma écrit dans le message de groupe de
discussion :
11**********************************... glegroups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
>On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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

--
Tom Shelton

Hello -

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

Thanks,
Joe

Nov 6 '08 #6
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

"Joe HM" <un*******@yahoo.comwrote in message
news:11**********************************@1g2000pr d.googlegroups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:
On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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

--
Tom Shelton
Hello -

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

Thanks,
Joe

Nov 6 '08 #7
On Nov 6, 1:40*pm, "Cor Ligthert[MVP]" <Notmyfirstn...@planet.nl>
wrote:
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

"Joe HM" <unixve...@yahoo.comwrote in message

news:11**********************************@1g2000pr d.googlegroups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:


On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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
--
Tom Shelton

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

Nov 6 '08 #8
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" <un*******@yahoo.comschreef in bericht
news:c3**********************************@w1g2000p rk.googlegroups.com...
On Nov 6, 1:40 pm, "Cor Ligthert[MVP]" <Notmyfirstn...@planet.nl>
wrote:
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

"Joe HM" <unixve...@yahoo.comwrote in message

news:11**********************************@1g2000pr d.googlegroups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@comcastXXXXXXX.net>
wrote:


On 2008-11-06, Joe HM <unixve...@yahoo.comwrote:
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
--
Tom Shelton

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
Nov 7 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Dave Smithz | last post by:
Hi there, A PHP application I built has a section which lists a number of members to a club whose names each appear with a check box beside them that can be ticked. These check boxes are part...
11
by: tshad | last post by:
I have the following: sub submitQuestion_click(Sender as Object, e as EventArgs) Dim submitButton as Button = CType(sender,Button) This gives me an error that says: "...
4
by: Lucas Tam | last post by:
Hi all, I like to dynamically check if an object implements a particular interface. Currently I am doing: Ctype(Object, IInterface) If the object implements the interface it will not...
13
by: Douglas Buchanan | last post by:
I am using the following code instead of a very lengthly select case statement. (I have a lot of lookup tables in a settings form that are selected from a ListBox. The data adapters are given a...
2
by: Marcus Kwok | last post by:
I was following the tutorial given at: http://msdn.microsoft.com/netframework/programming/winforms/?pull=/library/en-us/dndotnet/html/mdiappsmenus.asp except writing Managed C++ instead of...
5
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
2
by: Rickard | last post by:
I want to dynamically check if I can assign from type A to type B in runtime. Type.IsAssignableFrom does not work for me as, typeof(int).IsAssignableFrom(typeof(short)) returns false, which is...
1
by: zhang | last post by:
what's the problem?? Remote_Addr = "hotmail.com" sFrom = "<makefriend8@" & Remote_Addr + ">" Dim oConnection As New TcpClient() Try oConnection.SendTimeout = 3000 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.