Results 1 to 5 of 5

Thread: No TryParse?

  1. #1

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: No TryParse?

    Hi,
    why not use the val function - will return 0 for a non-numeric

    Pete
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width