|
-
Aug 21st, 2012, 11:17 AM
#1
Thread Starter
Member
Help to implement a function in a simple project
Hi,
My project has one goal: find interest rate, when loan,period
And payment are given.
Not knowing how to do this, I found,in the net, a function that calculates it.
But unfortunateley,I’m not yet familiar with functions and,therefore,
I don’t know how to implement it in this simple project
Please help.
Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
Dim loan As Double = "175 000.00"
Dim periods As Integer = "60" 'months'
Dim interestRate As Double 'value to retrieve'
Dim payment As Double = "3 941.59"
Double.TryParse(txtLoan.Text, loan)
Integer.TryParse(txtPeriods.Text, periods)
Double.TryParse(txtRate.Text, interestRate)
Double.TryParse(txtPayment.Text, payment)
End Sub
‘BELOW THE FUNCTION THAT RETRIVES INTEREST RATE.
‘I FOUND IT IN THIS LINK:
http://www.planet-source-code.com/vb...2422&lngWId=10
Function ApproxRate(ByVal Periods As Double, ByVal Payment As Double, ByVal loan As Double) As Double
Dim y As Double = 0
Dim n As Double = Periods
Dim z As Double = Payment / loan
Dim x As Double = 1.1
While (Math.Floor((y - 1) * 10000) <> Math.Floor((x - 1) * 10000))
y = x
x = y - ((Math.Pow(y, n + 1) - (Math.Pow(y, n) * (z + 1)) + z) / (((n + 1) * Math.Pow(y, n)) - (n * (z + 1) * Math.Pow(y, n - 1))))
End While
Return CDbl(x - 1)
End Function
-
Aug 21st, 2012, 11:44 AM
#2
Lively Member
Re: Help to implement a function in a simple project
Code:
interestRate = ApproxRate(Periods, Payment, loan)
now the variable interestRate holds the value that was return from the ApproxRate function..
The parameters in the function (stuff in the parentheses --->ApproxRate(Periods, Payment, loan) ) are the values that you declared before:
Code:
Dim loan As Double = "175 000.00"
Dim periods As Integer = "60" 'months'
Dim interestRate As Double 'value to retrieve'
Dim payment As Double = "3941.59"
if you wouldve instead done this:
Code:
Dim loany As Double = "175 000.00"
Dim fiveYearPeriod As Integer = "60" 'months'
Dim interestRate As Double 'value to retrieve'
Dim giveUpMoney As Double = "3941.59"
then you wouldve called the function like this:
Code:
ApproxRate(fiveYearPeriod, giveUpMoney, loany)
but you need to use or assign the value the function returns like this:
Code:
interestRate = ApproxRate(fiveYearPeriod, giveUpMoney, loany)
So the function will get those value, execute the code inside of the function and return a value of type Double...
Functions always give you a value back.. So you have to use or assign that value to something..
Subs just execute some code to make something happen..
You can google datatype, function, and Sub for vb.net....
-
Aug 21st, 2012, 12:11 PM
#3
Thread Starter
Member
Re: Help to implement a function in a simple project
Thank you very much. Yes, thank a lot for your help.
The function is well implemented.
However, it seems that it's not a good function,because it returns no valid rate.
please, if you have any hint or clue of how this could be resolved, let me know.
-
Aug 21st, 2012, 12:35 PM
#4
Lively Member
Re: Help to implement a function in a simple project
your variable loan and payment have spaces in them..
Numbers do not have spaces..
a type double is a classification for a number in which it is allowed to hold decimals to a min and max mark..
remove the spaces, no commas either.. just a pure decimal number....
-
Aug 21st, 2012, 05:36 PM
#5
Re: Help to implement a function in a simple project
For that matter, you are assigning strings to those variables initially, which makes no sense:
Dim loan As Double = "175 000.00"
Dim periods As Integer = "60" 'months'
Dim interestRate As Double 'value to retrieve'
Dim payment As Double = "3 941.59"
should be written as:
Dim loan As Double = 175000
Dim periods As Integer = 60 'months'
Dim interestRate As Double 'value to retrieve'
Dim payment As Double = 3941.59
However, you then fill those variables from textbox entries using TryParse, which is correct, except that you never check the return from TryParse. If you entered something into the textbox that TryParse can't turn into the proper type (and a comma, space, or dollar sign could do that), then TryParse will return False, and the variable will be 0.
You need to write those as:
Code:
If Double.TryParse(txtLoan.Text,loan) Then
'It was a number, so all is well.
Else
'It was not a number, so you probably want to put up a messagebox to tell the user that, then exit the sub.
End If
My usual boring signature: Nothing
 
-
Aug 21st, 2012, 05:39 PM
#6
Thread Starter
Member
Re: Help to implement a function in a simple project
I hurried to tell the function doesn't work.
It works very well. When it turns rate value, one should multiply it by 1200 (if payment is monthly,for example) to get
annually rate.
Anyway, again thank you and thank you.
-
Aug 21st, 2012, 06:15 PM
#7
Lively Member
Re: Help to implement a function in a simple project
nice catch on the strings shaggy... :Thumbs:
there you go.. thats how you do it.. very good...
didnt have to get into explaining the difference in how strings and integers take up and are allocated set amount of bytes for space in memory or anything....
for that i commend you...
but wouldnt of been a bad idea to briefly describe why it makes no sense.. Simple differences in data types...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|