If Else trouble

unable to validate

Page 1 of 1

3 Replies - 779 Views - Last Post: 17 November 2009 - 02:14 PM Rate Topic: -----

#1 barleybo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-November 08

If Else trouble

Post icon  Posted 16 November 2009 - 07:44 PM

I am creating a simple mortgage calculator and everything was fine untill I had to add validation to my text boxes. Now I cannot get the calculation to work. The previous post on this topic arent helping me due to the fact that I'm not supposed to use anything but If Else Statements.


Option Explicit On
Option Strict On
Public Class MortgageCalc

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
		txtAmount.Text = ""
		txtRate.Text = ""
		txtYears.Text = ""
		lblMonthlyPayment.Text = ""
		txtAmount.Focus() ' Reset focus on btnCalc						
	End Sub

	Private Sub btnEscape_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEscape.Click
		Me.Close()	  ' Quit Program
		btnCalc.Focus() ' Reset focus on btnCalc 
	End Sub

	Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
		Dim decAmount As Decimal
		Dim decMonths As Decimal
		Dim decRate As Decimal
		Dim decResult As Decimal
		Dim decPercentage As Decimal

		If Not Integer.TryParse(txtAmount.Text, CInt(decAmount)) Then
			
		End If
		If decAmount < 0 Then
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid data!")

		Else : decAmount = CDec(decAmount.ToString)
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid!")
		End If

		If Not Integer.TryParse(txtYears.Text, CInt(decMonths)) Then
			
		End If
		If decMonths < 0 Then
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid data!")

		Else : decMonths = CDec(decAmount.ToString)
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid!")
		End If

		If Not Integer.TryParse(txtRate.Text, CInt(decRate)) Then

		End If
		If decRate < 0 Then
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid data!")

		Else : decMonths = CDec(decAmount.ToString)
			txtRate.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid!")
		End If

		decMonths = decMonths * 12		 ' convertion to months		  
		decPercentage = decRate / 100 / 12 ' setting up formula to find perentage

		decResult = Convert.ToDecimal(decAmount * decPercentage * ((1 + decPercentage) ^ decMonths) / ((1 + decPercentage) ^ decMonths - 1))
		'formula to calculate a monthly amoritized amount 
		lblMonthlyPayment.Text = decResult.ToString("c")

		btnClear.Focus()
		txtAmount.Focus()  'set focus to textbox


	End Sub

	
End Class





Is This A Good Question/Topic? 0
  • +

Replies To: If Else trouble

#2 bigbug  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 97
  • Joined: 27-October 09

Re: If Else trouble

Posted 16 November 2009 - 08:33 PM

Not sure what your getting as a result but something I see is (if something < 0) but what about the value 0? Less than 0 and 0 are still nothing and should not be passed, in this case. The way you have it coded, 0 can pass to the else code and into your math. Try <= 0 or similar.

Give a little more info on what is happening that you don't like?

bigbug
Was This Post Helpful? 0
  • +
  • -

#3 mark.bottomley  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: If Else trouble

Posted 16 November 2009 - 09:08 PM

several random things:
1)
Integer.TryParse(txtYears.Text, CInt(decMonths))

should not have the CInt and if this succeeds in conversion, the value is now placed in decMonths - no further conversion attempts necessary!

2)you have failure message boxes on BOTH sides of the if/else clauses.
		If decMonths < 0 Then
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid data!")

		Else : decMonths = CDec(decAmount.ToString)
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid!")
		End If

lose the else half:
		If decMonths < 0 Then
			txtAmount.Focus()
			MessageBox.Show("The data you enter must be positive, numeric data. Please re-enter valid data!")
		End If



3) ALL failures detected in the data input should be followed by
Exit Sub
as you don't want to continue calculations with bad inputs.

that should get you close to back on track...
Was This Post Helpful? 1
  • +
  • -

#4 barleybo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-November 08

Re: If Else trouble

Posted 17 November 2009 - 02:14 PM

I finished this project successfully; thanks for the help! I ended up making one message box for any bad data using Decimal.TryParse.() And Decimal.TryParse() etc... and put a catch for numbers only > 0.0
all is well.

This post has been edited by barleybo: 17 November 2009 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1