473,666 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check wether CType() will throw InvalidCastExce ption?

Hello -

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

I could put a Try/Catch As System.InvalidC astException 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 4957
On 2008-11-06, Joe HM <un*******@yaho o.comwrote:
Hello -

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

I could put a Try/Catch As System.InvalidC astException 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...@co mcastXXXXXXX.ne t>
wrote:
On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? *I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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...@yaho o.comwrote:
On Nov 6, 11:37*am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:


On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? *I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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.comwrot e:
On Nov 6, 7:07*pm, Joe HM <unixve...@yaho o.comwrote:


On Nov 6, 11:37*am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:
On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? *I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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*******@yaho o.coma écrit dans le message de groupe de
discussion :
11************* *************** **...legroups. com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:
>On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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*******@yaho o.comwrote in message
news:11******** *************** ***********@1g2 000prd.googlegr oups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:
On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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...@yaho o.comwrote in message

news:11******** *************** ***********@1g2 000prd.googlegr oups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:


On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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(lObjectDu mmy, String)
Catch lException As System.InvalidC astException
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.toStri ng

Cor

"Joe HM" <un*******@yaho o.comschreef in bericht
news:c3******** *************** ***********@w1g 2000prk.googleg roups.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...@yaho o.comwrote in message

news:11******** *************** ***********@1g2 000prd.googlegr oups.com...
On Nov 6, 11:37 am, Tom Shelton <tom_shel...@co mcastXXXXXXX.ne t>
wrote:


On 2008-11-06, Joe HM <unixve...@yaho o.comwrote:
Hello -
I was wondering that the "cleanest" way is to determine whether a
CType() will throw an InvalidCastExce ption? I have data I receive as
an Object and I want to convert it to a String whenever possible using
CType(lObjectDu mmy, String) otherwise I will just ignore it.
I could put a Try/Catch As System.InvalidC astException 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(lObjectDu mmy, String)
Catch lException As System.InvalidC astException
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
2758
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 of a form which amongst other submit buttons has a one particular submit button that if checked will send the form data (using GET) to a script that will email all the checked members.
11
1638
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: " System.InvalidCastException: Specified cast is not valid. " Why is that?
4
12381
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 throw an error... Is there a better way I can do this via System.Reflection?
13
2286
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 similar name to the table. Rather than making a long Select Case that could become obsolete if lookup tables are added and the source table of the ListBox is edited I came up with this code.) This code works but of course it gives me build...
2
1451
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 VB.NET. In particular, under "Using Menu Groups", they have this code to implement radio-button-like functionality in menu items: Private Sub RadioCheck_Click( _ ByVal sender As System.Object, _
5
10461
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
6
2202
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 and resource consuming Try... Catch, which in iterative processing is totally unacceptable. -tom
2
2733
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 incorrect in the (english) meaning of "Is assignable from". Is there a function that checks if the assignment will throw an InvalidCastException?
1
1342
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 oConnection.Connect(mserver, 25) ' msserver is right for
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8639
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.