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.

Recommended Answers

All 5 Replies

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.

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

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?

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.