anyone familiar with Integer.TryParse ?

I have a strange problem with trying to parse a string from a textbox was wondering if anyone can figure this out. I have two code samples. The first doesn't work . The second does. The first sample will simply skip over the If statement and enter the Else statement. The second sample will enter the If statement and compile properly. Thanks for the help!

'This method DOESN'T WORK

Private Sub Compare()

Dim sw As Stopwatch

Dim outputString As String

compareBtn.Enabled = False

iterationsTextBox.Text = String.Empty

stringResultsTextBox.Text = String.Empty

stringBuilderResultsTextBox.Text = String.Empty

ratioLabel.Text = String.Empty

Me.Refresh()

'This doesn't work when Integer.TryParse is recieving the string for its

'first parameter from the text property of the iterationsTextBox

Dim iterations As Integer

'--------------Integer.TryParse-------------

If Integer.TryParse(iterationstextbox.Text, iterations) Then

sw = Stopwatch.StartNew

For i As Integer = 1 To iterations

outputString = TestString()

Next

sw.Stop()

Dim stringResults As Long = sw.ElapsedTicks

stringResultsTextBox.Text = sw.Elapsed.ToString

sw = Stopwatch.StartNew

For i As Integer = 1 To iterations

outputString = TestStringBuilder()

Next

sw.Stop()

Dim stringbuilderresults As Long = sw.ElapsedTicks

stringBuilderResultsTextBox.Text = sw.Elapsed.ToString

ratioLabel.Text = String.Format("Ratio: {0:G4}:1", (stringResults / stringbuilderresults))

Else

MessageBox.Show("Enter the number of iterations to perform")

End If

compareBtn.Enabled = True

End Sub

'This method DOES WORK

Private Sub Compare()

Dim sw As Stopwatch

Dim outputString As String

compareBtn.Enabled = False

iterationsTextBox.Text = String.Empty

stringResultsTextBox.Text = String.Empty

stringBuilderResultsTextBox.Text = String.Empty

ratioLabel.Text = String.Empty

Me.Refresh()

'This DOES WORK when Integer.TryParse is recieving the string for its

'first parameter from " Dim hardcodedstring As String = "10" "

Dim iterations As Integer

Dim hardcodedstring As String = "10"

'--------------Integer.TryParse-------------

If Integer.TryParse(hardcodedstring, iterations) Then

sw = Stopwatch.StartNew

For i As Integer = 1 To iterations

outputString = TestString()

Next

sw.Stop()

Dim stringResults As Long = sw.ElapsedTicks

stringResultsTextBox.Text = sw.Elapsed.ToString

sw = Stopwatch.StartNew

For i As Integer = 1 To iterations

outputString = TestStringBuilder()

Next

sw.Stop()

Dim stringbuilderresults As Long = sw.ElapsedTicks

stringBuilderResultsTextBox.Text = sw.Elapsed.ToString

ratioLabel.Text = String.Format("Ratio: {0:G4}:1", (stringResults / stringbuilderresults))

Else

MessageBox.Show("Enter the number of iterations to perform")

End If

compareBtn.Enabled = True

End Sub




Answer this question

anyone familiar with Integer.TryParse ?

  • kawano1h

    to me the first code looks like you do not have anything in the textbox variable as you are setting it to be String.Empty - there is nothing there therefore nothing to parse and it fails:

     



    iterationsTextBox.Text = String.Empty

    Dim iterations As Integer

    If Integer.TryParse(iterationstextbox.Text, iterations) Then --iterationsTextBox is empty with "", therefore it fails


     

     

    the second code will work because you are doing a tryparse on the "hardcoded" variable as that variable has something to try to parse

     



    Dim hardcodedstring As String = "10"

    '--------------Integer.TryParse-------------

    If Integer.TryParse(hardcodedstring, iterations) Then --succeeds as hardcodedstring has a value


     



  • Gabriel3

    A little question: I try use Integer.TryParse but my VB 2003 tell me that "'TryParse' is not member of 'Integer'", what's wrong
  • sebastian_v_b

    Yes that was it! This problem had me stumped. Thanks.

  • KannanPV

    Hi, Ntc,

    This is a new method in VS2005/.NET 2.0, so you won't be able to use it in VS2003.

    --Matt--*



  • anyone familiar with Integer.TryParse ?