VB 2008 TryParse Method

Can't get code to deliver error message when integer not used

Page 1 of 1

2 Replies - 7554 Views - Last Post: 10 March 2009 - 06:36 AM Rate Topic: -----

#1 La_Fille  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 09

VB 2008 TryParse Method

Post icon  Posted 10 March 2009 - 01:00 AM

The code performs correctly when the input is an integer, but when I enter a character, the result is as though I've entered an integer. Can you tell me what's wrong with my TryParse statement?

 

Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click

		' Declare Local Variables

		Dim intValueA As Integer
		Dim intValueB As Integer

		'Determine whether the input is an integer.

		If Integer.TryParse(txtValueA.Text, intValueA) _
		AndAlso Integer.TryParse(txtValueB.Text, intValueB) Then

			' ok to use intValueA and intValueB variables now
		Else
			lblResults.Text = "Error:  Please enter an integer."
		End If

		If intValueA = intValueB Then
			lblResults.Text = "The value of ""A"" is equal to the value of B."""

		ElseIf intValueA > intValueB Then
			lblResults.Text = "The value of ""A"" is greater than the value of B."""

		ElseIf intValueB > intValueA Then
			lblResults.Text = "The value of ""B"" is greater than the value of A."""

		End If

	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: VB 2008 TryParse Method

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: VB 2008 TryParse Method

Posted 10 March 2009 - 02:36 AM

You have to consider the ordering of the lines of code. lblResults.Text is being overwritten the next is used.

 

Private Sub btnCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompare.Click
' Declare Local Variables
Dim intValueA As Integer
Dim intValueB As Integer
'Determine whether the input is an integer.
If Integer.TryParse(txtValueA.Text, intValueA) AndAlso Integer.TryParse(txtValueB.Text, intValueB) Then
 ' ok to use intValueA and intValueB variables now
 If intValueA = intValueB Then
		lblResults.Text = "The value of ""A"" is equal to the value of B."""
 ElseIf intValueA > intValueB Then
lblResults.Text = "The value of ""A"" is greater than the value of B."""
 Else intValueB > intValueA Then
			lblResults.Text = "The value of ""B"" is greater than the value of A."""
endif

	Else
			lblResults.Text = "Error:  Please enter an integer."
		End If
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub


Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: VB 2008 TryParse Method

Posted 10 March 2009 - 06:36 AM

TryParse == .NET. Moving to VB.NET forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1