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``````

## All 3 Replies

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:

``````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

If CDbl(PresentVal) > 10000.00 Then
MessageBox.Show("Present Value must not be larger than 10000")
End Sub
End If
If CDbl(interest) < 2 Or CDbl(interest) > 10 Then
MessageBox.Show("Interest Rate must be between 2 and 10")
End Sub
End If
If CInt(period) < 3 Then
MessageBox.Show("The Period must be at least 3 years")
End Sub
End If

FutureVal = FV(PresentVal, interest, period)
lblFV.Text = FormatCurrency(FutureVal)
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.

commented: A GREAT HELP THANK YOU !! +2

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``````

thanks it worked bcasp, much appreciated !!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.