| | |
if statement problems
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 58
Reputation:
Solved Threads: 0
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.
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)
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
•
•
Join Date: Apr 2008
Posts: 45
Reputation:
Solved Threads: 10
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:
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.
vb Syntax (Toggle Plain Text)
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.
Last edited by bcasp; Nov 26th, 2008 at 2:36 pm.
•
•
Join Date: Nov 2008
Posts: 63
Reputation:
Solved Threads: 10
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:
formcode:
code below:
VB.NET Syntax (Toggle Plain Text)
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:
VB.NET Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- JavaScript Switch Statement Problems (JavaScript / DHTML / AJAX)
- User interface problems (Java)
- switch/case statement (C++)
- ASP.NET: Problems with CustomValidator... (ASP.NET)
- BASIC FORM problems... (PHP)
- compiling problems (C++)
- Cable Problems (Networking Hardware Configuration)
Other Threads in the VB.NET Forum
Views: 1126 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
.net .net2008 2008 access add advanced application array basic beginner browser button buttons center class click client code combo convert cpu cuesent data database datagrid datagridview datetimepicker design designer dissertation dissertations dissertationtopic eclipse employees excel exists forms function html images lib listview map mobile module msaccess mysql net number open page pan pdf picturebox picturebox2 port position print printing printpreview problem refresh regex reuse richtextbox right-to-left save search serial settings socket sorting sqldatbase sqlserver storedprocedure studio textbox timer timespan transparency txttoxmlconverter usercontol vb vb.net vb2008 vba vbnet vista visual visualbasic visualbasic.net visualstudio.net visualstudio2008 web webbrowser winsock wpf wrapingcode xml year





