4 Replies - 1774 Views - Last Post: 18 December 2009 - 07:45 AM Rate Topic: -----

#1 b4rox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 04-December 09

TryParse

Post icon  Posted 17 December 2009 - 10:37 AM

I am trying to check this app. and make sure only numbers are placed into the textbox, but my tryparse isn't working. Can you help me


 Private Sub btnConvertC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertC.Click
		Dim intTemp As Integer		  'holds the text input

		If Integer.TryParse(txtTemp.Text, intTemp) Then
			MessageBox.Show("You didn't enter a number!")
		Else
			intTemp = txtTemp.Text * 1.8 + 32   'Calc conversion
		End If
		lblDegreeConversion.Text = txtTemp.Text & " Degrees Celsius Converts to " & intTemp & " Degrees Fahenheit"
	End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: TryParse

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2253
  • View blog
  • Posts: 9,439
  • Joined: 29-May 08

Re: TryParse

Posted 17 December 2009 - 10:46 AM

The logic is the wrong way round TryParse returns True if is succeeds.

Also I've move question into the vb.net forum because the code you've supplied is vb.net not vb6.
Was This Post Helpful? 1
  • +
  • -

#3 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: TryParse

Posted 17 December 2009 - 11:59 AM

View Postb4rox, on 17 Dec, 2009 - 09:37 AM, said:

I am trying to check this app. and make sure only numbers are placed into the textbox, but my tryparse isn't working. Can you help me


 Private Sub btnConvertC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertC.Click
		Dim intTemp As Integer		  'holds the text input

		If Integer.TryParse(txtTemp.Text, intTemp) Then
			MessageBox.Show("You didn't enter a number!")
		Else
			intTemp = txtTemp.Text * 1.8 + 32   'Calc conversion
		End If
		lblDegreeConversion.Text = txtTemp.Text & " Degrees Celsius Converts to " & intTemp & " Degrees Fahenheit"
	End Sub



You probably want to move to following so it only executes if TryParse returns true.
lblDegreeConversion.Text = txtTemp.Text & " Degrees Celsius Converts to " & intTemp & " Degrees Fahenheit"


Was This Post Helpful? 0
  • +
  • -

#4 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 176
  • View blog
  • Posts: 990
  • Joined: 22-April 09

Re: TryParse

Posted 17 December 2009 - 09:33 PM

Normally, you would out an "Exit Sub" after the error message box to avoid any further execution.
Was This Post Helpful? 0
  • +
  • -

#5 ZRonZ  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 206
  • Joined: 09-January 09

Re: TryParse

Posted 18 December 2009 - 07:45 AM

View Postmark.bottomley, on 17 Dec, 2009 - 08:33 PM, said:

Normally, you would out an "Exit Sub" after the error message box to avoid any further execution.


mark.bottomley is absolutely correct. The reasoning for my comment was that I was trained to minimize code and in a simple Sub like this, moving one line eliminates adding one. Just my 2 cents.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1