MarcIT MarcIT - 1 year ago 129
C# Question

C# ignore empty textboxes

I have ha a WinForms app with some

At the moment the app is simple and i only want to test how it will work.
It will get mutch more complex in the future.
What i tried to do create a rule to ignore empty
or if empty use the value 0.
The code i have is this one:

decimal ata;
decimal a1;
decimal a2;
decimal a3;
decimal a4;
decimal a5;
decimal j = 0;

a1 = decimal.TryParse(textBox1.Text, out j);
a2 = decimal.Parse(textBox2.Text);
a3 = decimal.Parse(textBox4.Text);
a4 = decimal.Parse(textBox10.Text);
a5 = decimal.Parse(textBox24.Text);
ata = a1 + a2 + a3 + a4 + a5;

resposta.Text = ata.ToString();

i get an error "Cannot implicitly convert type 'bool' to 'decimal'
in the line:

a1 = decimal.TryParse(textBox1.Text, out j);

Can anyone help me with this problem.

Thanks in advance,

Answer Source

Use decimal.TryParse(textBox1.Text, out j); instead of a1 = decimal.TryParse(textBox1.Text, out j);. Your value is returned in j

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download