compucrazy
compucrazy

Reputation: 65

Discount and Interest rate program in vb.net

I am trying to figure out how to finish this program and get the calculations to display in the textboxes. This is what I have so far and basically the code will take the purchase price and the redemption rate and calculate the discount and interest rate. Not sure what I am doing wrong though.

Option Strict On

Public Class _Default
    Inherits System.Web.UI.Page

    Dim Purchase As Double
    Dim Redemption As Double
    Dim DiscountRate As Double
    Dim InterestRate As Double

    Protected Sub btnCalculate_Click(sender As Object, e As EventArgs) _
                                            Handles btnCalculate.Click

        Double.TryParse(txtPurchase.Text, Purchase)
        Double.TryParse(txtRedemption.Text, Redemption)
        Double.TryParse(txtDiscount.Text, DiscountRate)
        Double.TryParse(txtInterest.Text, InterestRate)

        If (CDbl(txtPurchase.Text) <= 0) Then
            MsgBox("Please enter an amount greater than 0")

        End If

        If (CDbl(txtRedemption.Text) <= 0) Then
            MsgBox("Please enter an amount greater than 0")
        End If

        DiscountRate = Purchase - Redemption / Purchase
        InterestRate = Purchase - Redemption / Redemption

    End Sub
End Class

Upvotes: 0

Views: 1233

Answers (2)

Mark Hall
Mark Hall

Reputation: 54532

It looks like you are having a problem with Operator Precedence you need some brackets. You are dividing your Redemption by purchase in the first case then subtracting it from your Purchase. In the second you are dividing redemption by redemption and subracting the result which is 1 from your purchase. Try this. You also need to assign the result to the control that you are displaying.

DiscountRate = (Purchase - Redemption) / Purchase
InterestRate = (Purchase - Redemption) / Redemption
txtDiscount.Text = DiscountRate.ToString
txtInterest.Text = InterestRate.ToString

or more simply

txtDiscount.Text = ((Purchase - Redemption) / Purchase).ToString
txtInterest.Text = ((Purchase - Redemption) / Redemption).ToString

Upvotes: 1

jamesbar2
jamesbar2

Reputation: 614

They variables aren't automatically bound to the control when you parse them. Looks like you just need to add this after your calculations:

txtDiscount.Text = DiscountRate
txtInterest.Text = InterestRate

Upvotes: 0

Related Questions