if statement problems
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.
Public Class Form1
Public Function FV(ByVal PV As Object, ByVal i As Object, ByVal n As Object) As Object
'Formula to calculate Future Value(FV)
'PV denotes Present Value
FV = PV * (1 + i / 100) ^ n
End Function
Private Sub compute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles compute.Click
'This procedure will calculate Future Value
Dim FutureVal As Decimal
Dim PresentVal As Decimal
Dim interest As Object
Dim period As Object
PresentVal = PV.Text
interest = rate.Text
period = years.Text
FutureVal = FV(PresentVal, interest, period)
lblFV.Text = FormatCurrency(FutureVal)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
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:
Public Class Class1
Private _FutureValue As Decimal
Private _PresentVal As Decimal
Private _Interest As Double
Private _Period As Integer
'just for remembering the formula (for me :=) )
Private Const CN_Formula As String = "FV = PV * (1 + i / 100) ^ n"
Public Property FutureValue() As Decimal
Get
Return _FutureValue
End Get
Set(ByVal value As Decimal)
_FutureValue = value
End Set
End Property
Public Property PresentVal() As Decimal
Get
Return _PresentVal
End Get
Set(ByVal value As Decimal)
_PresentVal = value
End Set
End Property
Public Property Interest() As Double
Get
Return _Interest
End Get
Set(ByVal value As Double)
_Interest = value
End Set
End Property
Public Property Period() As Integer
Get
Return _Period
End Get
Set(ByVal value As Integer)
_Period = value
End Set
End Property
Public Function Calculate() As Decimal
Try
Return Me.PresentVal * (1 + Me.Interest / 100) ^ Me.Period
Catch ex As Exception
Throw New ArgumentException("For some reason no calculation could be done!")
End Try
End Function
Public Sub New()
MyBase.New()
End Sub
End Class
formcode:
Public Class Form1
Private myCalc As Class1 = New Class1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
myCalc.PresentVal = CType(DirectCast(sender, TextBox).Text, Decimal)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
Try
myCalc.Period = CType(DirectCast(sender, TextBox).Text, Double)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Try
myCalc.Interest = CType(DirectCast(sender, TextBox).Text, Integer)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Me.TextBox4.Text = myCalc.Calculate
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
4advanced
Junior Poster in Training
67 posts since Nov 2008
Reputation Points: 33
Solved Threads: 10
thanks it worked bcasp, much appreciated !!
bpacheco1227
Junior Poster in Training
58 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0