-
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.
-
Re: No TryParse?
Hi,
why not use the val function - will return 0 for a non-numeric
Pete
-
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
-
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.
-
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.