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 StringcompareBtn.Enabled =
FalseiterationsTextBox.Text =
String.EmptystringResultsTextBox.Text =
String.EmptystringBuilderResultsTextBox.Text =
String.EmptyratioLabel.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) Thensw = Stopwatch.StartNew
For i As Integer = 1 To iterationsoutputString = TestString()
Nextsw.Stop()
Dim stringResults As Long = sw.ElapsedTicksstringResultsTextBox.Text = sw.Elapsed.ToString
sw = Stopwatch.StartNew
For i As Integer = 1 To iterationsoutputString = TestStringBuilder()
Nextsw.Stop()
Dim stringbuilderresults As Long = sw.ElapsedTicksstringBuilderResultsTextBox.Text = sw.Elapsed.ToString
ratioLabel.Text =
String.Format("Ratio: {0:G4}:1", (stringResults / stringbuilderresults)) ElseMessageBox.Show(
"Enter the number of iterations to perform") End IfcompareBtn.Enabled =
True End Sub
'This method DOES WORK Private Sub Compare() Dim sw As Stopwatch Dim outputString As String
compareBtn.Enabled =
FalseiterationsTextBox.Text =
String.EmptystringResultsTextBox.Text =
String.EmptystringBuilderResultsTextBox.Text =
String.EmptyratioLabel.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) Thensw = Stopwatch.StartNew
For i As Integer = 1 To iterationsoutputString = TestString()
Nextsw.Stop()
Dim stringResults As Long = sw.ElapsedTicksstringResultsTextBox.Text = sw.Elapsed.ToString
sw = Stopwatch.StartNew
For i As Integer = 1 To iterationsoutputString = TestStringBuilder()
Nextsw.Stop()
Dim stringbuilderresults As Long = sw.ElapsedTicksstringBuilderResultsTextBox.Text = sw.Elapsed.ToString
ratioLabel.Text =
String.Format("Ratio: {0:G4}:1", (stringResults / stringbuilderresults)) ElseMessageBox.Show(
"Enter the number of iterations to perform") End IfcompareBtn.Enabled =
True End Sub

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
sebastian_v_b
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--*