Results 1 to 7 of 7

Thread: Help to implement a function in a simple project

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    41

    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

  2. #2
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    41

    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.

  4. #4
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2012
    Posts
    41

    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.

  7. #7
    Lively Member elielCT's Avatar
    Join Date
    Jun 2011
    Posts
    89

    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
  •  



Click Here to Expand Forum to Full Width