Page 1 of 1

TryParse Rate Topic: -----

#1 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 07 November 2013 - 09:52 PM

TryParse

This has to do with the concept of parsing the contents of a string into a useable value.

Let's assume that we what to parse an Integer value.

What about CInt?

Dim text  As String  = "10"
Dim value As Integer = CInt(text)


Value contains 10.

But you're assuming that the text contains (or that user enters) valid information, so what happens if we use invalid data.
For example an empty string. ""

Dim text  As String  = ""
Dim Value As Integer = CInt( text )


What does that do?
Spoiler


What about using Integer.Parse
Dim text =""
Dim value = Integer.Parse(Text)


Spoiler



So why use TryParse?

{T}.TryParse parse a string into a value for a specific type {T} but not does that but returns a Boolean (True/False) value to indicate that parsing produce a valid value. Note that it doesn't return the value as the function return type, but passes it back via the referenced variable supplied as the argument for the result parameter.

The basic pattern for .TryParse is a following, where {T} is replaced by the type.

Dim Value As {T} = Nothing
If {T}.TryParse( Text , Value ) Then
   ' Is a valid parse and thus a valid value.
Else
  ' Not Valid
End If



This doesn't throw an Exception or produce an error, for invalid text.
It allows you to act on the valid parsing information to do something for invalid string content.
If you check for False or Invert the output you can have of all of your validations at the start of the method. Once you know you've got valid date proceed to process or act on it.
Dim a As Integer
Dim b As Integer
If (Not Integer.TryParse( NumberA.Text, a )) OrElse (Not Integer.TryParse( NumberB.Text, b )) Then
   MessageBox.Show("Invalid input") 
Else
  '  
End If



A lot of the type that parses a string input into a value provide a .TryParse function.
For example
Integer
Double
Int32
UInteger
Date
DateTime



This is why when you are parsing a string to a value (especially from user input) it is recommended that you use the .TryParse methods.

Is This A Good Question/Topic? 1
  • +

Replies To: TryParse

#2 bergeronjc  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-October 13

Posted 13 November 2013 - 12:17 PM

So with the below code, TryParse is trying to convert what is declared in text into the integer equivalent. Is that correct? I think it is, but I just want to make sure I'm understanding it correctly.

        Dim text As String = ""
        Dim value As Integer = Nothing
        If Integer.TryParse(text, value) Then
        Else
        End If

Was This Post Helpful? 0
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1291
  • View blog
  • Posts: 2,859
  • Joined: 30-January 11

Posted 13 November 2013 - 12:21 PM

Yes.
Was This Post Helpful? 0
  • +
  • -

#4 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 141
  • Joined: 22-September 09

Posted 24 November 2013 - 06:18 AM

View Postbergeronjc, on 13 November 2013 - 12:17 PM, said:

So with the below code, TryParse is trying to convert what is declared in text into the integer equivalent. Is that correct? I think it is, but I just want to make sure I'm understanding it correctly.

        Dim text As String = ""
        Dim value As Integer = Nothing
        If Integer.TryParse(text, value) Then
        Else
        End If


Technically, I’m not sure if the statement “convert text into integer” is correct. I’d incline more for “convert numeric text value to integer”.
In your example, an empty text (“”) fills the integer value with 0 simply because “” is not a numeric text value. The variable value is also filled with 0 if the text is “-“.
If TryParse would convert “” into an integer, would be its ASCII/(char() equivalent value.
 Dim value As Integer

        Dim text As String = "" 'text value
        If Integer.TryParse(text, value) Then
        Else : MsgBox(value.ToString) 'value = 0 
        End If

        text = "-" 'text value
        If Integer.TryParse(text, value) Then
        Else : MsgBox(value.ToString) 'value = 0 
        End If

        text = "Hello!" 'text value
        If Integer.TryParse(text, value) Then
        Else : MsgBox(value.ToString) 'value = 0 
        End If

        text = "Hello 5" 'text value
        If Integer.TryParse(text, value) Then
        Else : MsgBox(value.ToString) 'value = 0 
        End If

        text = "5" 'text value
        If Not Integer.TryParse(text, value) Then
        Else : MsgBox(value.ToString) 'value = 5 
        End If


Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2885
  • View blog
  • Posts: 9,570
  • Joined: 12-December 12

Posted 24 November 2013 - 06:31 AM

ebolisa
This is a little mis-leading. The values are not filled by 0 for "", "-", etc.. The attempts at TryParse fail (except for "5") and so value is set to 0. If the TryParse attempts were successful you wouldn't see any MsgBox at all (apart from the last one for "5").

This post has been edited by andrewsw: 24 November 2013 - 06:48 AM

Was This Post Helpful? 0
  • +
  • -

#6 ebolisa  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 141
  • Joined: 22-September 09

Posted 24 November 2013 - 09:26 AM

Thank you Andrew, that’s exactly my point. From my test code, a number is displayed (returned) only when the text contains only numbers otherwise, no data (0) is returned by the TryParse method. That’s why I didn’t think “convert text into integer” was the right nomenclature.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Posted 01 December 2013 - 01:18 AM

The .TryParse does return some value when it can not parse input string. It just that it me invalid (or more likely the default value for the type).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1