Pregunta

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.

¿Fue útil?

Solución

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;
    });

Otros consejos

I'd change the code so that you're leveraging collections rather than dealing with so many similar variables individually.

Create a dictionary that maps each currency value to the textbox that represents it:

var mapping = new Dictionary<decimal, TextBox>()
{
    {.05, textBox1},
    {.10, textBox2},
    {.20, textBox3},
    {.50, textBox4},
    {1, textBox5},
    {2, textBox6},
    //...
};

Then you can get out those values Where the text is a valid integer and Sum them.

int n;
decimal total = mapping.Where(pair => int.TryParse(pair.Value.Text, out n))
    .Sum(pair => pair.Key * int.Parse(pair.Value.Text));

You can use int.TryParse which will load your variable with 0 in case of non numeric data (including empty string).

int a = 0;
int.TryParse(textBox1.Text, out a);

You can use Int32.TryParse, but you can combine that with an extension method to clean up your code:

public static Int32 NumberOrZero(this string input)
{
    Int32 output = 0;

    if (!string.IsNullOrWhiteSpace(input))
    {
        Int32.TryParse(input, out output);
    }

    return output;
}

Then you can do:

Int32 aa = textBox1.Text.NumberOrZero(); // 5 c
Int32 ba = textBox2.Text.NumberOrZero(); // 10 c
Int32 ca = textBox3.Text.NumberOrZero(); // 20 c
Int32 da = textBox4.Text.NumberOrZero(); // 50 c
Int32 ea = textBox5.Text.NumberOrZero(); // 1 e
Int32 fa = textBox6.Text.NumberOrZero(); // 2 e
Int32 ga = textBox7.Text.NumberOrZero(); // 5 e
Int32 ha = textBox8.Text.NumberOrZero(); // 10 e
Int32 ia = textBox9.Text.NumberOrZero(); // 20 e
Int32 ja = textBox10.Text.NumberOrZero(); // 50 e
Int32 ka = textBox11.Text.NumberOrZero(); // 100 e
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top