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: txtLargestInvoice txtSmallestInvoice txtMidPoint I'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? asked 12/16/2011 02:56 |
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 |
it is a not an assignment - its is a continuation of an exercise in the book I'm using. the invoices are not being stored The 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 Decimal Dim smallestInvoice As Decimal Dim midPoint as decimal If 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 |
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 |
Here's what I changed under Public Class: 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 answered 2011-12-16 at 12:05:47 |
You have only 1 total so how would you calculate all those values from 1 value? answered 2011-12-16 at 12:12:20 |
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 |
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 |
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 |
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 |
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 |
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 Sub End Class answered 2011-12-16 at 13:32:51 |