none
What is the Basic Definition Of "TryParse"

Answers

  • Hi Theresonlyoneash,

    There are 2 ways you can use TryParse too, or 3 if you count doing a test and getting
     the result in one go as a 3rd way of using TryParse.

    I will show in code with comments the 3 ways you can use TryParse.


    Happy coding from,

    Tattooed Bloke.



    Add 1 Button to a Form to try this code please.




    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Example 1 that combines both a test and assignment to a DOUBLE variable.
            Dim InputString As String
            Dim AssignedDouble As Double
    
            Do
                InputString = InputBox("1) Please enter a number that includes a decimal point E.G. 3.5", "Example 1.")
                '1) Here the TryParse checks the InputString to see if it can be assigned to
                'the DOUBLE variable named 'AssignedDouble'
                'in doing so, TryParse will assign By Reference any VALID value.
            Loop Until Double.TryParse(InputString, AssignedDouble) = True And InputString.Contains(".") = True
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AssignedDouble.ToString, "Example 1 result.")
    
            '###############################################
    
            'Example 2.
            Dim InputString2 As String
            Dim AssignedInteger As Integer = 0
    
            Do
                InputString2 = InputBox("2) Please enter a number that DOES NOT include a decimal point E.G. 3456 and greater than zero.", "Example 2.")
                '2) Used on its own TryParse will attempt to assign the STRING
                'to the number type. If successful the assigned numeric value is the
                'value of the string even if you type 0 ( that is zero not the letter O ).
                'If you type a non-valid STRING the return result is zero.
                Integer.TryParse(InputString2, AssignedInteger)
            Loop Until AssignedInteger > 0
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AssignedInteger.ToString, "Example 2 result.")
    
            '###############################################
    
            'Example 3.
    
            'You could just use TryParse to test only and Parse to do the assignment
            'on the right-hand-side of an '=' assignment operation,
            'but I think that is overkill when code as in Example 1 works fine.
            Dim InputString3 As String
            Dim AnotherAssignedInteger As Integer = 0
    
            Do
                InputString3 = InputBox("3) Please enter a number that DOES NOT include a decimal point E.G. 3456 and greater than zero.", "Example 3.")
                '3) Here this code checks to see if the string is valid as an INTEGER
                'then it assigns it using Parse if the TryParse test is successful.
                If Integer.TryParse(InputString3, AnotherAssignedInteger) = True Then
                    AnotherAssignedInteger = Integer.Parse(InputString3)
                End If
            Loop Until AnotherAssignedInteger > 0
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AnotherAssignedInteger.ToString, "Example 3 result.")
    
        End Sub
    
    End Class
    Wednesday, October 07, 2009 2:32 AM

All replies

  • It will do exactly what the name suggests.

    It will TRY to PARSE a string as a numerical value.  It actually "returns" two separate objects.
    It returns a boolean field to indicate if the attempt was successful, and it "returns" the object you passsed as a parameter to hold the final result.


    Rudedog  =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Tuesday, October 06, 2009 8:02 PM

  • here is a link to the MSDN page on .tryParse

    http://msdn.microsoft.com/en-us/library/system.double.tryparse(VS.96).aspx


    a good way to read about these...

    if you have, for example, Double.tryParse in your code, click somewhere within TryParse, and then hit F1

    that will take you to the MSDN help page for that method, object, etc
    Tuesday, October 06, 2009 8:03 PM
  • I also did a blog entry on it:

    http://msmvps.com/blogs/deborahk/archive/2009/08/11/tryparse.aspx

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Tuesday, October 06, 2009 8:42 PM
  • Hi Theresonlyoneash,

    There are 2 ways you can use TryParse too, or 3 if you count doing a test and getting
     the result in one go as a 3rd way of using TryParse.

    I will show in code with comments the 3 ways you can use TryParse.


    Happy coding from,

    Tattooed Bloke.



    Add 1 Button to a Form to try this code please.




    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            'Example 1 that combines both a test and assignment to a DOUBLE variable.
            Dim InputString As String
            Dim AssignedDouble As Double
    
            Do
                InputString = InputBox("1) Please enter a number that includes a decimal point E.G. 3.5", "Example 1.")
                '1) Here the TryParse checks the InputString to see if it can be assigned to
                'the DOUBLE variable named 'AssignedDouble'
                'in doing so, TryParse will assign By Reference any VALID value.
            Loop Until Double.TryParse(InputString, AssignedDouble) = True And InputString.Contains(".") = True
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AssignedDouble.ToString, "Example 1 result.")
    
            '###############################################
    
            'Example 2.
            Dim InputString2 As String
            Dim AssignedInteger As Integer = 0
    
            Do
                InputString2 = InputBox("2) Please enter a number that DOES NOT include a decimal point E.G. 3456 and greater than zero.", "Example 2.")
                '2) Used on its own TryParse will attempt to assign the STRING
                'to the number type. If successful the assigned numeric value is the
                'value of the string even if you type 0 ( that is zero not the letter O ).
                'If you type a non-valid STRING the return result is zero.
                Integer.TryParse(InputString2, AssignedInteger)
            Loop Until AssignedInteger > 0
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AssignedInteger.ToString, "Example 2 result.")
    
            '###############################################
    
            'Example 3.
    
            'You could just use TryParse to test only and Parse to do the assignment
            'on the right-hand-side of an '=' assignment operation,
            'but I think that is overkill when code as in Example 1 works fine.
            Dim InputString3 As String
            Dim AnotherAssignedInteger As Integer = 0
    
            Do
                InputString3 = InputBox("3) Please enter a number that DOES NOT include a decimal point E.G. 3456 and greater than zero.", "Example 3.")
                '3) Here this code checks to see if the string is valid as an INTEGER
                'then it assigns it using Parse if the TryParse test is successful.
                If Integer.TryParse(InputString3, AnotherAssignedInteger) = True Then
                    AnotherAssignedInteger = Integer.Parse(InputString3)
                End If
            Loop Until AnotherAssignedInteger > 0
    
            MessageBox.Show("The PARSED value is.... " & vbNewLine & vbNewLine & AnotherAssignedInteger.ToString, "Example 3 result.")
    
        End Sub
    
    End Class
    Wednesday, October 07, 2009 2:32 AM
  • Hello again Theresonlyoneash,

    If you know HEXADECIMAL and OCTAL you can enter such a string preceded with

    &H for HEXADECIMAL such as &HFF

    &O for OCTAL such as &O377

    into an InputBox and if you use VAL instead and convert the result you will get the appropriate value.

    Note though that the &H is not part of the converted value nor
                       that the &O is not part of the converted value.

    They just indicate to the compiler that the following text may be hexadecimal
     / octal as appropriate as in

    Dim SomeNumber As Integer = &H135F

    VAL is short for VALUE and some would say that you should stick with TryParse, only TryParse
     does not accept Hexadecimal or Octal (VB.Net-code-format) strings such as

    &HFADE

    &O1417

    Here is code for you to try it out with one button on a Form.



    Enjoy coding,

    Tattooed Bloke


    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim InputString As String
            Dim AssignedInteger As Integer
    
            Do
                InputString = InputBox("Try typing any whole number greater than zero.")
                AssignedInteger = Convert.ToInt32(Val(InputString))
            Loop Until AssignedInteger > 0
    
            MessageBox.Show("Converted value = " & AssignedInteger.ToString)
    
        End Sub
    
    End Class
    Wednesday, October 07, 2009 3:10 AM