# Programming help VB 2010 Max, Min, Mid

 1 The code below is what I have done so far and it appears to work. Now I am challenged in this exercise to enhance my code as follows:I have addd three labels and three text controls. The text controls are as follows:txtLargestInvoicetxtSmallestInvoicetxtMidPointI'm ask to find the smallest invoice value, the largest invoice value and the midpoint using the Math Max,Min,Mid methods. If I could get someone to show the code for anyone of these methods I should be able to apply that logic and complete the other two by myself (note: this is not any type of homework, just an exercise from a book I'm using for VB 2010). I would guess that I need to define three months variables - largestInvoice, smallestInvoice, midPoint as decimals in the Public Class section to start off with? ` `Public Class frmInvoiceTotal Dim numberOfInvoices As Integer Dim totalOfInvoices As Decimal Dim invoiceAverage As Decimal Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim subtotal As Decimal = CDec(txtEnterSubtotal.Text) Dim discountPercent As Decimal = 0.25D Dim discountAmount As Decimal = Math.Round(subtotal * discountPercent, 2) Dim invoiceTotal As Decimal = subtotal - discountAmount txtSubtotal.Text = FormatCurrency(subtotal) txtDiscountPercent.Text = FormatPercent(discountPercent, 1) txtDiscountAmount.Text = FormatCurrency(discountAmount) txtTotal.Text = FormatCurrency(invoiceTotal) numberOfInvoices += 1 totalOfInvoices += invoiceTotal invoiceAverage = totalOfInvoices / numberOfInvoices txtNumberOfInvoices.Text = numberOfInvoices.ToString txtTotalOfInvoices.Text = FormatCurrency(totalOfInvoices) txtInvoiceAverage.Text = FormatCurrency(invoiceAverage) txtEnterSubtotal.Text = "" txtEnterSubtotal.Select() End Sub` ` `1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: ` asked 12/16/2011 02:56

 1 Math.Max would return the max number out of two. How many invoices do you have? Where are these stored? Is it an assignment? answered CodeCruiser
 1 it is a not an assignment - its is a continuation of an exercise in the book I'm using.the invoices are not being storedThe invoice amount is calulated and diplayed in the control txtTotal.I quess the best way to help me understand the requirements of this application is to start with variable definition. I'm assuming that I need to create 3 Public class variables first. Dim largestInvoice As DecimalDim smallestInvoice As DecimalDim midPoint as decimalIf that's ok, I'll make changes to the code I posted furst, close and assign the points and then open a continuation as I run into more questions. Is that ok? answered 2011-12-16 at 11:20:38 fh_freese
 1 Yes but how would you calculate the largest, smallest, and midpoint? You can store invoice amounts in sorted list and get those values. A DataTable would be useful as well. answered 2011-12-16 at 11:29:25 CodeCruiser
 1 This exercise doesn't take me there at this point. OK....going forward All the code is what I've posted in the beginning. I'm now challenged to add max, min and mid methd. Ok going forward with Dim statements? answered 2011-12-16 at 11:55:39 fh_freese
Here's what I changed under Public Class:
 ```1: 2: 3: 4: 5: 6: 7: 8:``` ```Public Class frmInvoiceTotal Dim numberOfInvoices As Integer Dim totalOfInvoices As Decimal Dim invoiceAverage As Decimal Dim largestInvoice As Decimal = CDec(txtTotal.Text) Dim smallestInvoice As Decimal = CDec(txtTotal.Text) Dim midPoint As Decimal = CDec(txtTotal.Text)```

I added the last three variables. My data comes from a text box call txtEnterSubtotal.Text and is the only input source. Whe I click on the calculate button the code I posted in the beginning is executed. Somewhere in that code I need to get the Min,Max,MidPoint and assign those values to their text boxes txtLargestInvoice, txtSmallestInvoice and txtMidPoint

fh_freese

 1 You have only 1 total so how would you calculate all those values from 1 value? answered 2011-12-16 at 12:12:20 CodeCruiser
 1 Well here's what I'm being ask to learn"Add three more labels and text boxes. The labels read "Largest invoice", Smallest invoice" and "Mid Point". The text bozes to the right of the labels should display the values for the largest invoice total, the samllest invoice total and the invoice that's halfway between thos totals. " (I've set all this up)"Add the code that makes this work (Hint; To find the smallest invoice total, use Math.Min nethod to compare each invoice total to a variable that contains the smallest invoice to that point. Then replace the variable value with the smaller of the two invoice totals. To make this work for your first invoice, you can initialize this variable to a number that will be larger than the largest invoice total, like 9999999). You now know what I know.....thanks answered 2011-12-16 at 12:14:14 fh_freese
 1 I know what its asking to do but you are saying that invoice total is coming from a textbox. That is one invoice total. The exercise is asking your to have many invoice totals so that you can find the max, min, and mid. Have you learned how to use arrays? answered 2011-12-16 at 12:28:18 CodeCruiser
 1 Array's are 3 chapters away. I guess all I'm being ask to do is compare the last invoice with the current invoice (2 invoices only) and find the Max, Min and Mid Point. So when I calculate the first time and then calcuate a second invoice I'm to find the MAX, Min and Midpoint answered 2011-12-16 at 12:31:38 fh_freese
 1 You can calculate the invoice value that is halfway in-between the min and max values....but to know the the mid-point value, that is the invoice value that is exactly in the middle (and was entered by the user), then you'd need to store all the values in an array or list.*You can certainly do the Min/Max, though, without storing all of them.  This can be done by either setting the Min/Max values to a sentinel value, or by using another variable (such as a boolean) to track whether the invoice entered is the very first entry. answered 2011-12-16 at 12:45:11 Idle_Mind
 1 ok....let's look at this from a different viewpoint. In my form I have a text box labled txtTotal that is the results of a caluagtion. I need to store that value in another variable. When I calculate for another invoice I need to determine if what I just calculated is Max or Min when compared to the first calculation and then calculate the midpoint of the two. What would the code look like and what variable(s) do I need to declare? answered 2011-12-16 at 12:53:40 fh_freese
 1 I'm not ware of any Math.Mid() function...This is what I think they want you to do:  *additions in bold*Public Class frmInvoiceTotal    Dim numberOfInvoices As Integer    Dim totalOfInvoices As Decimal    Dim invoiceAverage As Decimal    Private smallestInvoice As Decimal    Private largestInvoice As Decimal    Private Sub btnCalculate_Click(ByVal sender As System.Object,            ByVal e As System.EventArgs) Handles btnCalculate.Click        Dim subtotal As Decimal = CDec(txtEnterSubtotal.Text)        Dim discountPercent As Decimal = 0.25D        Dim discountAmount As Decimal = Math.Round(subtotal * discountPercent, 2)        Dim invoiceTotal As Decimal = subtotal - discountAmount        txtSubtotal.Text = FormatCurrency(subtotal)        txtDiscountPercent.Text = FormatPercent(discountPercent, 1)        txtDiscountAmount.Text = FormatCurrency(discountAmount)        txtTotal.Text = FormatCurrency(invoiceTotal)        numberOfInvoices += 1        totalOfInvoices += invoiceTotal        invoiceAverage = totalOfInvoices / numberOfInvoices        txtNumberOfInvoices.Text = numberOfInvoices.ToString        txtTotalOfInvoices.Text = FormatCurrency(totalOfInvoices)        txtInvoiceAverage.Text = FormatCurrency(invoiceAverage)        If numberOfInvoices = 1 Then            smallestInvoice = invoiceTotal            largestInvoice = invoiceTotal        Else            smallestInvoice = Math.Min(smallestInvoice, invoiceTotal)            largestInvoice = Math.Max(largestInvoice, invoiceTotal)        End If        txtSmallestInvoice.Text = FormatCurrency(smallestInvoice)        txtLargestInvoice.Text = FormatCurrency(largestInvoice)        txtMidPoint.Text = FormatCurrency((smallestInvoice + largestInvoice) / 2)        txtEnterSubtotal.Text = ""        txtEnterSubtotal.Select()    End SubEnd Class answered 2011-12-16 at 13:32:51 Idle_Mind
 1 that'll help...by the way it was a math.max method I was asking aboutthanks to all answered 2011-12-16 at 14:02:17 fh_freese
 [hide preview] community wiki

By Email: