|
-
Sep 15th, 2009, 09:43 AM
#1
No TryParse?
I was curious if there is a .TryParse() method in the Compact Framework? If not, is there any standardized method I should be handling string to numeric conversion? I mean, I know I can do this:
Code:
Private Overloads Function TryParse(ByVal s As String, ByRef i As Integer) As Boolean
Try
i = Convert.ToInt32(s)
Return True
Catch ex As Exception
Return False
End Try
End Function
Private Overloads Function TryParse(ByVal s As String, ByRef d As Decimal) As Boolean
Try
d = Convert.ToDecimal(s)
Return True
Catch ex As Exception
Return False
End Try
End Function
...but I've never been keen on handling functions with exceptions.
Thanks.
-
Sep 15th, 2009, 11:40 AM
#2
Frenzied Member
Re: No TryParse?
Hi,
why not use the val function - will return 0 for a non-numeric
Pete
-
Sep 15th, 2009, 12:01 PM
#3
Re: No TryParse?
Jenner - I'd be willing to guess that's exactly how TryParse works anyways, so why not?
pete - because it returns erroneous data, that's why. And it has a lot of shortcomings. and doesn't tell me if the input data is bad (tryParse will).
-tg
-
Sep 15th, 2009, 11:35 PM
#4
Re: No TryParse?
1. If you want to know whether the TryParse method is supported in the CF then all you have to do is read the documentation.
2. TryParse is definitely NOT implemented using an exception handler. In fact, the Double.TryParse method was created in the first place because the IsNumeric function, which originally did use an exception handler, performed so abysmally. It proved such a great addition that the TryParse family was expanded considerably in .NET 2.0. If you want to see how TryParse is implemented then you can use .NET Reflector.
3. If there's no TryParse method available then an exception handler is pretty much your only simple option. The alternative would be to write code similar to what the "official" TryParse methods contain, which is likely rather complex.
-
Sep 16th, 2009, 03:03 PM
#5
Re: No TryParse?
I did check the documentation and even found MSDN to be in error in one of it's pages stating that TryParse is supported in the Compact Framework. User comments below verified that it is not.
I was mostly wondering if there was a similar function elsewhere or existing in some different library.
Thus, since Microsoft didn't decide to "grace" the CF community with this all-too-useful function, I'm pretty much forced to go with what I got, since I don't feel like writing out an elaborate function to check strings byte by byte for proper formatting of numeric datatypes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|