I have a form that has people enter an amount, and it calculates a discount for the invoice and displays it, as well as total of all invoices, number of invoices, and average invoice amount. I need it to also display the smallest and largest invoices. I am still quite new to C#, so any hints would be greatly appreciated. Here's the code I have so far, not sure how to start on the last part, looking into using Math.Max and Math.Min.
I tend to prefer the second style, but either is fine.
Though: if you wrote the rest of that code, you should be entirely competent to see solution for yourself. We're happy to help, of course, but I think you will find these sorts of solutions will become apparent if you just work through them. MSDN is your friend. Here are, for example, the documents for Math.Min an Math.Max.
I've also surrounded your code with [code] and [/code] tags; this will preserve formatting, making it easier (and more likely) for us to read your code.
Hope that helps!
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
So, with your help I was able to get much further. I declared the variables for minInvoice and maxInvoice. I added the lines of code using the math.min and math.max syntax. I then attached the 2 variables to my text boxes for minimum and maximum invoice. The code runs, but it just displays the subtotal in the min and max invoice fields. I will continue working on it. I appreciate your help thus far.
Here you should be using TryParse() though, this is a textBox control i'm presuming, therefore invalid input can result in a runtime exception if the input is not validated first.
Realize, that this is all the ToDecimal() function does, for the string param overload:
Code:
public static decimal ToDecimal(string value)
{
if (value == null)
{
return 0M;
}
return decimal.Parse(value, CultureInfo.CurrentCulture);
}
A textBox's Text property, will NEVER be null, so really, all it's doing is calling decimal.Parse() for you, and without validation on the input. The most "nothingness" value a textBox can have is string.Empty.
minInvoice will ALWAYS be the invoiceTotal because minInvoice is as high as things get. There can be no larger decimal value than decimal.MaxValue, and you're comparing for the smallest of those 2 numbers. It doesn't make sense. It's the complete reverse with the Math.Max line, unless your invoiceTotal is a negative value, you can't get a smaller value than 0...
Last edited by AceInfinity; February 3rd, 2013 at 04:25 PM.
I see what you mean Ace. It was suggested I use MaxValue so the correct value is returned the first time. If I had declared it as 0, then the first time an invoice is entered it would always display 0 as the min invoice.
I see what you mean Ace. It was suggested I use MaxValue so the correct value is returned the first time. If I had declared it as 0, then the first time an invoice is entered it would always display 0 as the min invoice.
That's why if you look at my looping method, I automatically set it to the first invoice, and then I start looping from there, without looking at the one I already assigned as the minimum value to find any values smaller than the first invoice that I assigned to that variable. Doing it that way, avoids you from having to assign the variable to a useless value that doesn't really do anything for you.
**It double posted for some reason, so i'll provide an analogy here:
It would be like picking a number from a bag that has a whole bunch of paper slips, and then taking each other piece of paper out of that bag individually comparing to the first one you took out, and keeping the smaller value from each comparison as you went along until no more slips of paper were left.
Logically, this makes much more sense, than for instance, what you're doing, which is almost like having an initial piece of paper, writing a ridiculously huge number on it. And using that as a stepping stone to compare the same way as described earlier in my post here. Being able to skip steps is always good in programming, so long as you get a good reliable result.
I haven't gotten as far as loops yet. Here's what I am supposed to do:
Use the Math.Min method to compare each invoice total to a variable that contains the smallest invoice total to that point. Then, replace the variable with the smaller of the two invoice totals. To make this work for the first invoice, you can initialize this variable to a large number like the MaxValue member of the Decimal class.
This is a homework thing? Sort out your knowns for what they want you to do then:
- Use Math.Min() to compare each invoice total to a variable that contains the smallest invoice total
I wouldn't suggest setting it to MaxValue if there's no reason. Unless it's possible to get an invoice of $0, you can set it to the first invoice total, then you have an immediate starting point to compare against future invoices.
You know what you're supposed to do, but I can't help and I won't write code for you especially if this is homework, so what are you stuck on then?
I don't expect anyone to write code for me, just looking for outside opinions as well. My classmates in this online course aren't posting any suggestions either, and neither is my instructor. I thank you for your help anyways.
It seems to be doing the first invoice right, it should display invoiceTotal in both the smallest and largest invoice fields the first time. Just can't work out how to get it to compare the next input to the previous one. We cover loops in the next chapter, so I'm guessing I shouldn't be using those yet.
Bookmarks