943,682 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 2040
  • VB.NET RSS
Nov 26th, 2008
0

if statement problems

Expand Post »
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.

VB Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007
Nov 26th, 2008
1

Re: if statement problems

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:

vb Syntax (Toggle Plain Text)
  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.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008
Nov 26th, 2008
0

Re: if statement problems

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:
VB.NET Syntax (Toggle Plain Text)
  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:
VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 33
Solved Threads: 10
Junior Poster in Training
4advanced is offline Offline
67 posts
since Nov 2008
Nov 26th, 2008
0

Re: if statement problems

thanks it worked bcasp, much appreciated !!
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bpacheco1227 is offline Offline
58 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC