if statement problems

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

if statement problems

 
0
  #1
Nov 26th, 2008
I'm having problems with if statements
present value (PV) should be no larger than $10000
interest rate is not higher than 10 and no less than 2
and number of years at least 3

I've tried the if statements with error messageboxs but I was getting confused with all the if's and the messagebox didn't show up right. The following code is my code before entering the if's. Any insight would be very helpful, thank you.

  1. Public Class Form1
  2.  
  3. Public Function FV(ByVal PV As Object, ByVal i As Object, ByVal n As Object) As Object
  4. 'Formula to calculate Future Value(FV)
  5. 'PV denotes Present Value
  6. FV = PV * (1 + i / 100) ^ n
  7.  
  8. End Function
  9.  
  10. Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click
  11. 'This procedure will calculate Future Value
  12. Dim FutureVal As Decimal
  13. Dim PresentVal As Decimal
  14. Dim interest As Object
  15. Dim period As Object
  16.  
  17. PresentVal = PV.Text
  18. interest = rate.Text
  19. period = years.Text
  20. FutureVal = FV(PresentVal, interest, period)
  21. lblFV.Text = FormatCurrency(FutureVal)
  22.  
  23. End Sub
  24.  
  25.  
  26. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  27. Me.Close()
  28. End Sub
  29.  
  30.  
  31. End Class
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

Re: if statement problems

 
1
  #2
Nov 26th, 2008
I'm assuming you want to check the values before making the call to the FV function. If that's the case, you could accomplish what you're trying to do with three simple if statements. Something like the following would probably work just fine:

  1. Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click
  2. 'This procedure will calculate Future Value
  3. Dim FutureVal As Decimal
  4. Dim PresentVal As Decimal
  5. Dim interest As Object
  6. Dim period As Object
  7. PresentVal = PV.Text
  8. interest = rate.Text
  9. period = years.Text
  10.  
  11. If CDbl(PresentVal) > 10000.00 Then
  12. MessageBox.Show("Present Value must not be larger than 10000")
  13. End Sub
  14. End If
  15. If CDbl(interest) < 2 Or CDbl(interest) > 10 Then
  16. MessageBox.Show("Interest Rate must be between 2 and 10")
  17. End Sub
  18. End If
  19. If CInt(period) < 3 Then
  20. MessageBox.Show("The Period must be at least 3 years")
  21. End Sub
  22. End If
  23.  
  24. FutureVal = FV(PresentVal, interest, period)
  25. lblFV.Text = FormatCurrency(FutureVal)
  26. End Sub

If you just need something simple, this should work. You may want to change the conversions depending on what you're assuming will be input from the text fields, and you would obviously want to use some kind of exception handling anytime you're converting the text from user input into various data types.
Last edited by bcasp; Nov 26th, 2008 at 2:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 63
Reputation: 4advanced is an unknown quantity at this point 
Solved Threads: 10
4advanced 4advanced is offline Offline
Junior Poster in Training

Re: if statement problems

 
0
  #3
Nov 26th, 2008
The way I should implement is : using the LostFocus event for the textboxes. After the lostfocus you can warn a user corresponding the maximum or minimum values needed. Second is creating yourself a class in which you calculate the FV.

code below:
  1. Public Class Class1
  2. Private _FutureValue As Decimal
  3. Private _PresentVal As Decimal
  4. Private _Interest As Double
  5. Private _Period As Integer
  6. 'just for remembering the formula (for me :=) )
  7. Private Const CN_Formula As String = "FV = PV * (1 + i / 100) ^ n"
  8.  
  9. Public Property FutureValue() As Decimal
  10. Get
  11. Return _FutureValue
  12. End Get
  13. Set(ByVal value As Decimal)
  14. _FutureValue = value
  15. End Set
  16. End Property
  17. Public Property PresentVal() As Decimal
  18. Get
  19. Return _PresentVal
  20. End Get
  21. Set(ByVal value As Decimal)
  22. _PresentVal = value
  23. End Set
  24. End Property
  25. Public Property Interest() As Double
  26. Get
  27. Return _Interest
  28. End Get
  29. Set(ByVal value As Double)
  30. _Interest = value
  31. End Set
  32. End Property
  33. Public Property Period() As Integer
  34. Get
  35. Return _Period
  36. End Get
  37. Set(ByVal value As Integer)
  38. _Period = value
  39. End Set
  40. End Property
  41. Public Function Calculate() As Decimal
  42. Try
  43. Return Me.PresentVal * (1 + Me.Interest / 100) ^ Me.Period
  44. Catch ex As Exception
  45. Throw New ArgumentException("For some reason no calculation could be done!")
  46. End Try
  47.  
  48. End Function
  49.  
  50. Public Sub New()
  51. MyBase.New()
  52. End Sub
  53. End Class

formcode:
  1. Public Class Form1
  2. Private myCalc As Class1 = New Class1
  3. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  4. Try
  5. myCalc.PresentVal = CType(DirectCast(sender, TextBox).Text, Decimal)
  6. Catch ex As Exception
  7. MsgBox(ex.Message)
  8. End Try
  9. End Sub
  10.  
  11.  
  12. Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
  13. Try
  14. myCalc.Period = CType(DirectCast(sender, TextBox).Text, Double)
  15. Catch ex As Exception
  16. MsgBox(ex.Message)
  17. End Try
  18. End Sub
  19.  
  20. Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
  21. Try
  22. myCalc.Interest = CType(DirectCast(sender, TextBox).Text, Integer)
  23. Catch ex As Exception
  24. MsgBox(ex.Message)
  25. End Try
  26. End Sub
  27.  
  28. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  29. Try
  30. Me.TextBox4.Text = myCalc.Calculate
  31. Catch ex As Exception
  32. MsgBox(ex.Message)
  33. End Try
  34.  
  35. End Sub
  36. End Class
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 58
Reputation: bpacheco1227 is an unknown quantity at this point 
Solved Threads: 0
bpacheco1227 bpacheco1227 is offline Offline
Junior Poster in Training

Re: if statement problems

 
0
  #4
Nov 26th, 2008
thanks it worked bcasp, much appreciated !!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC