XTab's Blog

Ged Mead's Blog at vbCity

This blog hosted by:
http://blogs.vbcity.com      
  Home :: Syndication  :: Login

JunJuly 2008Aug
SMTWTFS
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Archives

Topics

Ramblings

VB.NET

   If you've used IsNumeric in the past and thought it was pretty handy, then you may be interested in TryParse, another nifty little method that's been slipped into VB2005 without much of a fanfare.

 

  TryParse takes a string and tries to parse it to a particular type.    It will return True or False to signify whether the attempt to parse has succeeded.    If it does succeed, then the method will pass back the parsed value as an output parameter.

 

   Of course there has been a Parse method around for ages, but if that original method failed then it would throw an Exception.     TryParse simply tries to parse the string, returns the Boolean result, but is good-natured about it if the parse fails.

 

   I didn't find the syntax of TryParse immediately intuitive and so thought it might be useful to devise an example to show it in action.    However, in the past few months I'd seen some really excellent examples of TryParse being used very efficiently and effectively.   So after spending several long hours kicking dead-end ideas into the waste bin, I came to the conclusion that I couldn’t hope to improve on those.

 

    The two examples that I made a note of when I saw them and thought "Must remember those!" were posted in the MSDN technical Forums.  One was posted by Alex Moura of Microsoft  and the other by Ken Tucker, MVP.

 

   In both cases they used TryParse to offer a really neat solution to a problem.

 

In the first example, the question was how to read filenames in a directory and extract the digits from the filenames.   Helpfully, the filenames were all formatted in the same way, two letters followed by four digits:

WW487487.exe

WW154874.exe

WW244874.exe

 

   Alex's answer used TryParse like this:

 

For Each file As String In System.IO.Directory.GetFiles("c:\", "ww??????.exe")

       Dim value As Integer

       'Cleanup full path to get the file name only

        file = IO.Path.GetFileName(file)

        If Integer.TryParse(file.Substring(2, 6), value) Then

            Console.WriteLine(value)

        End If

Next

 

     Ken’s fix was for a problem where a textbox would at some stage contain a date, ideally today’s date.     At other times the textbox might be empty.  TryParse used in this situation looked like this:

 

     Dim dt As Date

        If Date.TryParse(TextBox1.Text, dt) Then

            If dt = My.Computer.Clock.LocalTime.Date Then

                MessageBox.Show("Today is your day")

            End If

        End If

 

   Notice how in both cases, not only does the method attempt to parse, but that it also passes back that parsed value – an integer in the first example and a date in the second - when the parsing succeeds.

 

   So if you want a parse method with bells, whistles and quite a lot of flexibility you might want to give TryParse a run out.

 

   My thanks and acknowledgements to Alex and Ken.  

 

    The links to the full original questions are:

 

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=158084&SiteID=1

(Alex Moura)

 

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=214685&SiteID=1

(Ken Tucker)

 

   

posted on Saturday, March 25, 2006 11:40 AM