I have to count with money and I have created some textboxes where the user can input how many 1 euro coins he has. (and so for 2 euro, 5 euro, etc) Now I want to calculate to total, but if a textbox is empty, you can't count whit it. I have tried some ways but it won't work because the program will automatically start counting with every input.
To keep it short, I want to skip empty textboxes during calculation.
Now I have this:
Int32 aa = Int32.Parse(textBox1.Text); // 5 c
Int32 ba = Int32.Parse(textBox2.Text); // 10 c
Int32 ca = Int32.Parse(textBox3.Text); // 20 c
Int32 da = Int32.Parse(textBox4.Text); // 50 c
Int32 ea = Int32.Parse(textBox5.Text); // 1 e
Int32 fa = Int32.Parse(textBox6.Text); // 2 e
Int32 ga = Int32.Parse(textBox7.Text); // 5 e
Int32 ha = Int32.Parse(textBox8.Text); // 10 e
Int32 ia = Int32.Parse(textBox9.Text); // 20 e
Int32 ja = Int32.Parse(textBox10.Text); // 50 e
Int32 ka = Int32.Parse(textBox11.Text); // 100 e
Int32 total = ((aa * 5) + (ba * 10) + (ca * 20) + (da * 50) + (ea * 100) + (fa * 200) + (ga * 500) + (ha * 1000) + (ia * 2000) + (ja * 5000) + (ka * 100000)) / 100;
richTextBox1.AppendText("\r\nTotaal: € " + total.ToString());
But that doesn't work, because sometimes there are empty boxes ;)
I hope you can give me a simple solution.
I think you should use this approch:
var list = new List<Tuple<TextBox, decimal>>(){
Tuple.Create(textBox1, 0.05m),
Tuple.Create(textBox2, 0.1m),
Tuple.Create(textBox3, 0.2m),
Tuple.Create(textBox4, 0.5m),
Tuple.Create(textBox5, 1m),
Tuple.Create(textBox6, 2m),
Tuple.Create(textBox7, 5m),
Tuple.Create(textBox8, 10m),
Tuple.Create(textBox9, 20m),
Tuple.Create(textBox10, 50m),
Tuple.Create(textBox11, 100m),
};
decimal sum = list.Sum(tuple =>{
int a = 0;
int.TryParse(tuple.Item1.Text, out a);
return a * tuple.Item2;
});