Results 1 to 38 of 38

Thread: Help with formula for my project

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Help with formula for my project

    Hello I am a beginner in visual basic and I had to do my first project to calculate a monthly payment using principal, annual rate in decimal, and time in years, which are all controlled by horizontal scroll bars. I have everything set up except I can not figure out the formula for the monthly payment. Also I am required to have option explicit and strict on.
    The formula that i was given is this Name:  payment.jpg
Views: 577
Size:  15.1 KB
    where c is the monthly payment,
    r is the monthly interest rate, expressed as a decimal, not a percentage
    N is the number of monthly payments, called the loan's term,
    P is the amount borrowed, known as the loan's principal.

    The way that i calculated the annual rate is this:
    lblAnnualPercent.Text = CStr(0.1 * hsbAnnualRate.Value)
    The way that i calculated the principal is this:
    lblPrincipalAmount.Text = "$" & CStr(100 * hsbPrincipal.Value)
    And the way that I calculated the time is this:
    lblTimeYears.Text = CStr(hsbTime.Value)

    I tried a bunch of different ways to come up with the formula to calculate the monthly payment and I kept getting errors. Can anybody please help me?

  2. #2
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Help with formula for my project

    vb.net Code:
    1. Private Function CalculateMonthlyPayment()
    2.  
    3.         Dim r As Decimal = 0.15 '' intrest rate
    4.         Dim N As Int16 = 1     '' installment
    5.         Dim P As Single = 100 '' or may be double (@ @) borrowed amount
    6.         Dim C As Single = 0      '' Monthly payment
    7.  
    8.  
    9.         Dim Pr As Single = P * r
    10.         Dim PrRn As Single = (1 + r) ^ N '' r + 1 to the power of N
    11.         Dim Rn As Single = ((1 + r) ^ N) - 1 '' 1 + R tothe power of N -1
    12.  
    13.  
    14.  
    15.  
    16.         C = Pr * PrRn / Rn
    17.  
    18.  
    19.         Return C
    20.  
    21.  
    22.     End Function
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    Here I have used Decimal as the data type since this is about money. The one common term, (1 + r) ^ N is calculated once.

    Code:
            Dim r As Decimal = 0.05D / 12D '5% yearly as monthly rate
            Dim N As Decimal = 30D * 12D '30 years in months
            Dim P As Decimal = 100000D '$ 100,000
            Dim C As Decimal
    
            Dim OnePlusRN As Decimal = CDec((1D + r) ^ N) 'needed twice, calculate once
    
            C = (P * r * OnePlusRN) / (OnePlusRN - 1D)
    
            Dim cs As String = C.ToString("c2") 'payment as string
    Last edited by dbasnett; Feb 14th, 2016 at 05:59 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    I am so sorry guys I should have said in the first post but we are not to use any variables! We can only use object properties
    So for monthly payment I created a label called lblMPAmount so I guess my formula would start with lblMPAmount.Text = ...and then i have to use the Value properties from the scrollbars and also additional things and calculations which I am not sure what they would be.
    Last edited by xtreme1; Feb 14th, 2016 at 07:24 AM.

  5. #5
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Help with formula for my project

    We can only use object properties
    Means that
    you must have a class & properties such as amount , intrest rate , Installment and an OUT readonly property as what ever the EMI
    still you need variables in your class

    What kind of programming are you trying to do with out data , types and variables
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    I don't know it is a project for school and our instructor specifically told us to not use variables I am not sure why. I know that it would be a lot easier that way. I don't know it's due tonight so I have to figure something out soon. I guess I will wait and see if anybody else has any solutions

  7. #7
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Help with formula for my project

    definitely you must have mistaken,
    your instructor must have been told you to use the properties, as i have mentioned above
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  8. #8
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Help with formula for my project

    lets have a class with takes & exposes the properties ,
    vb.net Code:
    1. Public Class EmiCalculater
    2.  
    3.  
    4.     Private _IntrestRate As Decimal
    5.     Private _Installments As Decimal
    6.     Private _PrincipleAmount As Decimal
    7.  
    8.  
    9.     Public WriteOnly Property IntrestRate() As String
    10.         '' the value is string because its from text box
    11.  
    12.  
    13.         Set(value As String)
    14.             _IntrestRate = Convert.ToDecimal(value)
    15.         End Set
    16.  
    17.  
    18.     End Property
    19.  
    20.  
    21.     Public WriteOnly Property Installments() As String
    22.         '' the value is string because its from text box
    23.  
    24.  
    25.         Set(value As String)
    26.             _Installments = Convert.ToDecimal(value)
    27.         End Set
    28.  
    29.  
    30.     End Property
    31.  
    32.  
    33.     Public WriteOnly Property PrincipleAmount() As String
    34.         '' the value is string because its from text box
    35.  
    36.  
    37.         Set(value As String)
    38.             _PrincipleAmount = Convert.ToDecimal(value)
    39.         End Set
    40.  
    41.  
    42.     End Property
    43.  
    44.  
    45.     Private Function CalculateMonthlyPayment()
    46.  
    47.  
    48.         Dim C As Decimal = 0      '' Monthly payment
    49.  
    50.  
    51.         Dim Pr As Decimal = _PrincipleAmount * _IntrestRate
    52.         Dim PrRn As Decimal = (1 + _IntrestRate) ^ _Installments  '' Thanks to dbas (@-@) here
    53.  
    54.  
    55.         C = Pr * PrRn / PrRn - 1  '' Check your formula implementation here
    56.  
    57.  
    58.         Return C
    59.  
    60.  
    61.     End Function
    62.  
    63.  
    64.     Public ReadOnly Property WriteOutPutToLable() As String
    65.  
    66.  
    67.         Get
    68.             Return CalculateMonthlyPayment().ToString
    69.         End Get
    70.  
    71.  
    72.     End Property
    73.  
    74.  
    75. End Class
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  9. #9
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Help with formula for my project

    now assign & write the output to your lables
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.  
    3.         Dim Ecm As New EmiCalculater
    4.  
    5.  
    6.         With Ecm
    7.             .PrincipleAmount = 100 '' may be this value from any text box
    8.             .IntrestRate = 0.15      '' .... do ....
    9.             .IntrestRate = 1          ''
    10.             Me.Label1.Text = .WriteOutPutToLable  '' write the output to your lable
    11.         End With
    12.  
    13.  
    14.  
    15.  
    16.     End Sub
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  10. #10
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    Quote Originally Posted by xtreme1 View Post
    I don't know it is a project for school and our instructor specifically told us to not use variables I am not sure why. I know that it would be a lot easier that way. I don't know it's due tonight so I have to figure something out soon. I guess I will wait and see if anybody else has any solutions
    You should tell us exactly what the instructor told you. Writing a method without the use of variables is, IMHO, stupid, if not impossible. Hopefully your instructor is not that and you have just missed something.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  11. #11

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    Okay here are all the instructions for the project. Look at step number 5 specifically

    1. Name your project PaymentCalculator.
    2. Using this formula, you are to write a Visual Basic program that accepts as input the principal, rate, and time and uses the monthly payment formula to calculate the monthly loan payment.
    3. Additionally, you will calculate the total amount of interest. You do NOT have to look up a formula for this; simply use the values already given or calculated to determine this amount!
    4. As seen below, place your initials in the form's title bar. (Note: you will ALWAYS put your initials or name in title of each form. Follow the format, initials in this one, shown in each assignment.) This is the final message about this!
    5. You are NOT to use any variables - only use object properties! Be sure you name your objects using the Object Naming Convention discussed in the first PowerPoint slide show. This is the final message about this!
    6. Do NOT use any TextBoxes - only labels that look like TextBoxes.
    7. When executed, the Payment form appears in the center of the screen, and your form cannot be resized. (Note: This is the final message about this!)
    8. Attach your zipped VB project, including the executable file in an e-mail message with your name and section number at the top of the message, and sent it to the e-mail address used for this course

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    Are the objects in step 5 form controls? How long have you been in this class?
    Last edited by dbasnett; Feb 14th, 2016 at 04:56 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  13. #13

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    I am not sure what exactly he means in step 5 which is why I am so confused.. I have been in the class for about 3 weeks

  14. #14
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    Create a form as specified. On it create three textboxes named txtPrin, txtRate, txtTime. Also create two labels named lblPay, and lblTotInt. Double click the form and replace all of the code with this:

    Code:
    Public Class Form1
        Private Sub txtChanged(sender As Object, e As EventArgs) Handles _
            txtPrin.TextChanged, txtRate.TextChanged, txtTime.TextChanged
    
            Dim r As Decimal 'monthly rate
            Dim N As Decimal 'number of months
            Dim P As Decimal 'principal
            Dim C As Decimal 'monthly payment
            Dim err As Boolean = False
            If Decimal.TryParse(txtPrin.Text, P) AndAlso P > 0D Then
                'principal good
                If Decimal.TryParse(txtRate.Text, r) AndAlso r > 0D Then
                    'rate present - if the user input 5, instead of .05 for % fix it
                    If r > 1 Then r = r / 100D
                    r = r / 12D
                    If Decimal.TryParse(txtTime.Text, N) AndAlso N > 0D Then
                        'time good, have good values
                        N = N * 12 'years to months
                        Dim OnePlusRN As Decimal = CDec((1D + r) ^ N) 'needed twice, calculate once
                        C = (P * r * OnePlusRN) / (OnePlusRN - 1D)
                        lblPay.Text = C.ToString("c2")
                        lblTotInt.Text = (C * N - P).ToString("c2")
                    Else
                        'input error
                        err = True
                    End If
                Else
                    'input error
                    err = True
                End If
            Else
                'input error
                err = True
            End If
            If err Then
                lblPay.Text = ""
                lblTotInt.Text = ""
            End If
        End Sub
    End Class
    Enter values into the textboxes.

    Do me a favor. When you get the instructors code come back here and show it. I might also want their email. This is the final message about this! LOL
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  15. #15

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    Well the only problem is, I don't know how to integrate my scrollbars into your code.. The values of each one - principal, annual rate, time, etc.. they are supposed to change as you are scrolling. I already have those figured out the only output that I need is the one for monthly payment amount

  16. #16

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    I think he wants the formula to look something like this because I remember him saying in class that we are going to be using a lot of parenthesis for the formula.. I came up with this but it doesn't seem to be working
    Code:
    lblMPAmount.Text = ((lblPrincipalAmount.Text*(lblAnnualPercent.Text/12))*(1+(lblAnnualPercent.Text/12))^(lblTimeYears.Text/12))/((1+(lblAnnualPercent.Text/12))^((lblTimrYears.Text/12))-1)

  17. #17
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    There's no rule on this forum against doing people's homework for them. We tend not to because we recognise that it really doesn't do them (or our industry) any favours in the long run. Also, we do put them at risk of being accused of plagiarism and cheating even if that wasn't their intention. But if a user feels that a student is putting in the effort but needs a prod in the right direction there's nothing wrong with giving them such a prod.

    Xtreme1, I don't get the impression you came here looking for handouts. From your posts it's clear that you've already constructed your form with sliders etc. and are already outputting some of the simpler formulae. The technique to calculate and output the extra formulae is exactly the same as you're already doing.

    So this:-
    Code:
    lblAnnualPercent.Text = CStr(0.1 * hsbAnnualRate.Value)
    ...is taking the value of your annual rate slider, multiplying it by 0.1, converting the result to a string and outputting that string to you annual percent label.

    You know how to take values from sliders - just take the values you need and plug them into your formula. then convert the result to a string and output it to the appropriate label as you are with other values.

    Have a punt at it and see how you get on. You probably won't get it right first time but if you get stuck, post your code back here and we'll see if we can tell you what went wrong.
    Last edited by FunkyDexter; Feb 16th, 2016 at 08:46 AM.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  18. #18
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    I've deleted a bunch of posts because they were just argument. For clarification of our forum rules on what help we should or shouldn't give out here see my prior post.

    We all like to offer help in different ways but let's try not to derail someone thread arguing about it.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  19. #19
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Help with formula for my project

    To be honest, it does look like the hard part, here, is trying to figure out what the instructor is asking in the narrow confines of the outlines steps. It does sound like you haven't learned about classes, is this correct?

    What you could do, is build the equation using variables in separate lines, and construct the formula;
    Then, you would 'compress' or refactor, the formula back into a one liner that you stated isn't working in post #16.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  20. #20

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    I tried so many different ways and nothing is giving me the correct output which makes me think that either the formula is wrong or I am just not typing it correctly. The last thing I tried was to create a function and then just call that function every time I slide a scrollbar but it is not giving me the correct value.. Here's what I have
    Code:
    Function calculate() As Double
            Dim r As Double = (0.1 * hsbAnnualRate.Value) / 12
            Dim N As Double = hsbTime.Value * 12
            Dim P As Double = hsbPrincipal.Value * 100
            Dim c As Double = (P*r*(1+r)^N)/((1+r)^N-1)
            Return c
        End Function
    It is close to the correct value but a little bit off
    Last edited by xtreme1; Feb 16th, 2016 at 11:24 AM.

  21. #21
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Help with formula for my project

    you mentioned lots of brackets, but you have lost a number in your attempts so far

    the order by which the machine calculates your expressions is called the precedence BODMAS is the numonic for simple maths precedence Brackets Or Division, Multiplication, Addition then Subtraction this is the order you do a calculation.

    your (p*r*(1+r)^N) might be better as (p*r*((1+r^N)) also the ((1+r)^N-1) might be better as (((1+r)^N)-1)

    a simple way to test these is to use the immediate window in the IDE/SDK environment typr print followed by the expression, putting your actual values in the brackets.

    if you need to test while running place stop before and after the lines you want to test and then cursor over the values that you are wanting to test when running the program

    you can also add debug.print whatever to your code and see what is being produced when the program runs ( seen in the immediate window or msgbox whatever and a message box will pop up ( annoying but usefull)


    I had suggested you build a function, but was expecting something like this...

    Code:
    Function calculate(r,N,P) As Double
                  Return   (P*r*(1+r)^N)/((1+r)^N-1)
              End Function
    called as follows

    Code:
    result=calculate( 0.1 * hsbAnnualRate.Value) / 12, hsbTime.Value * 12, hsbPrincipal.Value * 100)
    although I am not at all sure of your units in that call - if you try this pass in the real values in the immediate window as a way of testing it

    NOTE I have not corrected any error here!

    here to help

  22. #22
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    @incidentals, I think you might have got your own brackets confused. Shouldn't this: (p*r*((1+r^N)) be this: (p*r*((1+r)^N) ? I'm assuming you added the extra parentheses to force the power to be applied before the multiplication.

    I don't think the parentheses are the problem, though. They look right to me but I'll freely admit I haven't been very thorough with my checking on that. Adding the extra pars might add clarity but I don't think it will change the result in this case. I suggest you check them thoroughly, though, as that will be an easy area to make a mistake on.

    I think my suggestion would be much the same as incidentals: step through the code and make sure each of the values (r, N, P) is what you're expecting. Other than that, do some research and check the formula is correct (it probably is) and that your expected answer is correct. From what I'm looking at I believe you've applied the formula correctly.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  23. #23
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Help with formula for my project

    you are quite correct I added the parameters kepping his for clarity and then lost one in the process

    I am also not sure of the precedence, but would always want to ensure the order that i wanted by using too many brackets!

    nice catch anyway thanks

    I hope the remarks about stops,debug and print/msgbox make sense to the OP

    here to help

  24. #24

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    Okay I finally got everything to work as my instructor emailed me with some more help. Unfortunately I did it using variables which I was not supposed to but it's better than nothing. Thank you everyone for the help!

  25. #25
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    I would still like to see how it is supposed to have been done.

    Code written like this (p*r*((1+r)^N) / ((1+r)^N-1) confuses me and is inefficient unless the compiler factors out the (1+r)^n. You might have less lines of code but the compiler wouldn't care if you did this one step at a time.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  26. #26

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    Yeah that is the only way that I could think of so I just used the formula that I posted up there but instead of 12 I divided by 1200 and it worked.. If he posts his solution then I will post it here!

  27. #27
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    I did it using variables
    I think more than one of us probably breathed a sigh of relief when we read that. I can understand why your instructor might not have wanted to introduce them yet but to ban them seemed a little crazy.

    instead of 12 I divided by 1200 and it worked
    Ah, so you were pulling the value for the monthly interest rate wrong. By the way, that probably means you could just take the formula as you had it in post 16 and change the 12 to a 1200 and get the right answer. That would mean you wouldn't be using variables. I personally think it would be a backward step but it might keep your instructor happy.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  28. #28
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    Quote Originally Posted by FunkyDexter View Post
    I think more than one of us probably breathed a sigh of relief when we read that. I can understand why your instructor might not have wanted to introduce them yet but to ban them seemed a little crazy...
    In one of the posts I asked how long the class has been in session. Call me crazy but three weeks in and variables haven't been introduced seems odd.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  29. #29
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    I'm inclined to agree but I could envisage someone wanting to teach the visual controls first. I guess you could spend 3 weeks + exploring all the options and properties. It's not the approach I would use and it does seem a little bass ackwards but it might not be completely crazy.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  30. #30
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Help with formula for my project

    Quote Originally Posted by FunkyDexter View Post
    I'm inclined to agree but I could envisage someone wanting to teach the visual controls first. I guess you could spend 3 weeks + exploring all the options and properties. It's not the approach I would use and it does seem a little bass ackwards but it might not be completely crazy.
    True, but spend a couple of lessons on the basics of EVERY language might be a better idea - I really don't know where they get these teachers from, or the course curriculum. That's why I would personally advocate C as the first step to any programming course. Or, at the very least, a console program first.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  31. #31

    Thread Starter
    New Member
    Join Date
    Feb 2016
    Posts
    12

    Re: Help with formula for my project

    Okay he posted the solution here is what the whole homework is supposed to look like
    Code:
    Option Explicit On
    Option Strict On
    
    Public Class frmMain
    
    	Private Sub hsbPrincipal_ValueChanged(sender As System.Object, e As System.EventArgs) Handles hsbPrincipal.ValueChanged
    		lblPrincipal.Text = FormatCurrency(hsbPrincipal.Value * 100, 0)
    		FindValues()
    	End Sub
    
    	Private Sub hsbRAte_ValueChanged(sender As System.Object, e As System.EventArgs) Handles hsbRate.ValueChanged
    		lblRate.Text = CStr(hsbRate.Value * 0.1)
    		FindValues()
    	End Sub
    
    	Private Sub hsbTime_ValueChanged(sender As System.Object, e As System.EventArgs) Handles hsbTime.ValueChanged
    		lblTime.Text = Cstr(hsbTime.Value)
    		FindValues()
    	End Sub
    
    	Private Sub FindValues()
    		lblPayment.Text = FormatCurrency((Cdbl(lblPrincipal.Text) * CDbl(lblRate.Text) /1200 * (1 + CDbl(lblRate.Text) / 1200) ^ (12 * CDbl(lblTime.Text))) / ((1+ CDbl(lblRate.Text) / 1200) ^ (12 * CDbl(lblTime.Text)) -1))
    		lblInterest.Text = FormatCurrency(CDbl(lblPayment.Text) * 12 * CDbl(lblTime.Text) - CDbl(lblPrincipal.Text))
    	End Sub
    End Class

  32. #32
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    What I expected. Did the instructor tell you what would happen if the users weren't perfect? Have the instructor show you what happens if the user accidentally enters letters into these 'numeric' fields.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  33. #33
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    I think they're scroll bars rather than text boxes so they're guaranteed to be numeric.

    I do know what you're driving at, though, and whole-heartedly agree. It would be nice if FindValues was two separate functions and was separated from the UI. Of course that would have involved not just variables but also parameters! Heaven forbid!

    xtreme1, I hope you get a decent mark because, from what I saw you made a pretty god fist of it. And your instructors solution is... troubling. If he's just introducing you to basic controls it might make sense but it is full of bad habits. Hopefully he's way ahead of us and will address some of those habits in future lessons. If not just keep hanging around here and we'll start drilling some better practices for you.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  34. #34
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Help with formula for my project

    Here's my deal about that code.

    I don't like it. I don't like the concept of teaching new developers that there's anything positive about it at all. If it came to one of my code reviews, I'd reject it with half a page of details for improvement. And it would lessen my opinion of whoever tried to submit it.

    A lot of times, people dismiss this kind of code as "good enough for examples" or "good enough for homework". Please don't treat it as the kind of code you should write. When you are in school, you get exposed to one of the worst truths of our industry: we're paid to write code that works, not code that is pretty.

    But ugly code is hard to maintain. We've spent 50 years of software development learning if we don't think hard about how to write our code elegantly, we usually end up having problems working on that code further down the road.

    If nothing else, pay attention to this:

    I don't like code that directly uses control values within formulas. No one should like it, and I will stubbornly declare anyone wrong that advocates for it. Let's pretend this is the smallest bit of the formula:
    Code:
    Cdbl(lblPrincipal.Text) * CDbl(lblRate.Text)
    This is a much better way to write that:
    Code:
    Dim principal = Double.Parse(lblPrincipal.Text)
    Dim rate = Double.Parse(lblRate.Text)
    
    Dim result = principal * rate
    Now, people can and will bicker about CDbl() vs. Double.Parse() vs. Double.TryParse(), but that's ignoring what's really important here. The second technique makes it explicit that we want a TextBox's text to be converted to a Double, and separates that bit of logic from the logic that performs calculations. Separating bits of code that do two different things is the most important skill you can learn.

    People can and will bicker about whether it's better to write fewer lines of code. I will again stubbornly declare them wrong, and I hold that opinion very strongly. It is always a good idea to write two lines of code if you want to do two things. When you are an expert with several years of experience, you will understand when that rule can be bent a little. To get there, you have to break it the wrong way a lot of times and get angry at yourself when it costs you an hour of debugging.

    So pick up this trick, and for now only break the rule when a professor asks you to do it. School isn't the last place your code will be graded for style: it will happen again in job interviews. But the expectations there are much higher. Look for a book called Clean Code by Robert C. Martin for some guidance there!
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  35. #35
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Help with formula for my project

    @Sitten - Amen! When the OP first posted I had a bad feeling for them. The 'no variables' set all kind of warning bells off.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  36. #36
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,470

    Re: Help with formula for my project

    The teachers coding is suseptable to empty strings in the text or labels used. the divisor can be made zero value and the calculation will explode under "division by zero error"

    under the line (1+0)^n-1 is zero therefore the calculation fails!

    here to suggest you take teacher to task for not checking for this problem.

  37. #37
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Help with formula for my project

    Still here to suggest you don't. He deserves it, but you don't want to be the messenger who get's shot for delivering bad news.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  38. #38
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Help with formula for my project

    Yeah, I don't recommend telling your teacher a bunch of people on an internet forum say his code's bogus. We're right, but he has a lot of power over you and it's in your best interests to make friends with him. True story: I got an A in a class I should've had a C in because I was the only student who didn't openly voice contempt for the teacher. Diplomacy's sometimes more useful than real knowledge.

    Here's what I recommend: if you can, shop around for a better school. You're probably spending real money on this, you may as well get your money's worth. If you don't have many options, whatever. My school classes only taught C++ and Java, and here I am 9 years later writing C#, VB, Swift, and sometimes HTML. If you don't mind reading books, you want a career in programming, and you want to look smarter than your classmates, I can suggest half a dozen books that'll get you there.

    It makes me sad how common this is of VB curriculums. It feels like a lot of schools are more worried about generating tuition than useful programmers.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

Tags for this Thread

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