0

Hi, I'm trying to store a new value within a variable and then compare it to the previous value of that variable using the Math.Min method. I have created two seperate variable 1 for the previous and one for the new but I can't seem to store it. They both keep coming up with the same amount. Any suggestions would be appreciated.

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by StuartMillner
1

That would be expected for reference types, but not for value types. Sounds like it could be an order-of-operation problem, don't you think? Post the code, let's see what's going on.

0

That would be expected for reference types, but not for value types. Sounds like it could be an order-of-operation problem, don't you think? Post the code, let's see what's going on.

Here is the code, didn't think of reference types, I'll give it a shot thanks!

Dim decSubtotal As Decimal
        Dim decDiscountPercent As Decimal = 0.25D
        Dim decDiscountAmount As Decimal
        Dim decInvoiceTotal As Decimal
        Dim decSmallestInvoiceTotal As Decimal

        decSubtotal = CDec(txtEnterSubtotal.Text)
        decDiscountAmount = Math.Round(decSubtotal * decDiscountPercent, 2)
        decInvoiceTotal = decSubtotal - decDiscountAmount


        decSmallestInvoiceTotal = Math.Min(decSmallestInvoiceTotal, decInvoiceTotal) 'comparison of stored value and next input value
        decSmallestInvoiceTotal = decInvoiceTotal 'this cannot be correct, would like it to retain decInvoiceTotal for next input

        txtSubtotal.Text = FormatCurrency(decSubtotal)
        txtDiscountPercent.Text = FormatPercent(decDiscountPercent, 1)
        txtDiscountAmount.Text = FormatCurrency(decDiscountAmount)
        txtTotal.Text = FormatCurrency(decInvoiceTotal)


        intNumberOfInvoices += 1
        decTotalOfInvoices += decInvoiceTotal
        decInvoiceAverage = decTotalOfInvoices / intNumberOfInvoices
        
        txtNumberOfInvoices.Text = intNumberOfInvoices.ToString
        txtTotalOfInvoices.Text = FormatCurrency(decTotalOfInvoices)
        txtInvoiceAverage.Text = FormatCurrency(decInvoiceAverage)
        txtSmallestInvoice.Text = FormatCurrency(decSmallestInvoiceTotal) 'want it to retain min amount.

        txtEnterSubtotal.Text = ""
        txtEnterSubtotal.Select()

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

1
Dim decSubtotal As Decimal
Dim decDiscountPercent As Decimal = 0.25D
Dim decDiscountAmount As Decimal
Dim decInvoiceTotal As Decimal
Dim decSmallestInvoiceTotal As Decimal

decSubtotal = CDec(txtEnterSubtotal.Text)
decDiscountAmount = Math.Round(decSubtotal * decDiscountPercent, 2)
decInvoiceTotal = decSubtotal - decDiscountAmount


decSmallestInvoiceTotal = Math.Min(decSmallestInvoiceTotal, decInvoiceTotal) 'comparison of stored value and next input value
decSmallestInvoiceTotal = decInvoiceTotal 'this cannot be correct, would like it to retain decInvoiceTotal for next input

txtSubtotal.Text = FormatCurrency(decSubtotal)
txtDiscountPercent.Text = FormatPercent(decDiscountPercent, 1)
txtDiscountAmount.Text = FormatCurrency(decDiscountAmount)
txtTotal.Text = FormatCurrency(decInvoiceTotal)


intNumberOfInvoices += 1
decTotalOfInvoices += decInvoiceTotal
decInvoiceAverage = decTotalOfInvoices / intNumberOfInvoices

txtNumberOfInvoices.Text = intNumberOfInvoices.ToString
txtTotalOfInvoices.Text = FormatCurrency(decTotalOfInvoices)
txtInvoiceAverage.Text = FormatCurrency(decInvoiceAverage)
txtSmallestInvoice.Text = FormatCurrency(decSmallestInvoiceTotal) 'want it to retain min amount.

txtEnterSubtotal.Text = ""
txtEnterSubtotal.Select()

I'm not sure what variable you're talking about, but let's look at lines 12 and 13 for a second.

decSmallestInvoiceTotal = Math.Min(decSmallestInvoiceTotal, decInvoiceTotal) 'comparison of stored value and next input value

decSmallestInvoiceTotal = decInvoiceTotal 'this cannot be correct, would like it to retain decInvoiceTotal for next input

If decSmallestInvoiceTotal is the variable you're having a problem with it's because you overwrote the value that Math.min returned by assigning decSmallestInvoiceTotal to another value (decInvoiceTotal) immediately afterwards. This makes the assignment

decSmallestInvoiceTotal = Math.Min(decSmallestInvoiceTotal, decInvoiceTotal)

completely pointless because it's never used, just rewritten immediately.

Edited by StuartMillner: n/a

0

Thanks!, My code is supposed to take the invoice total(decInvoiceTotal) and compare it to a new invoice total and populate the txtSmallestInvoice.Text and continue to change or keep the lowest value while using the Math.Min method. Any Suggestions?

0

txtsmallestinvoice.text = math.min(decinvoicetotal, other variable you want to compare)

and delete this line:

decSmallestInvoiceTotal = decInvoiceTotal 'this cannot be correct, would like it to retain decInvoiceTotal for next input
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.