0
\$\begingroup\$

Is this the correct way to set value of textbox to zero when it's null? Is there any way I can improve the calculation that I am doing?

private void TB_PAID_CASH_TextChanged(object sender, EventArgs e)
{
 try
 {
 if (!string.IsNullOrEmpty(TB_TOTAL_INV.Text) && !string.IsNullOrEmpty(TB_PAID_CASH.Text) && string.IsNullOrEmpty(TB_Discount.Text))
 {
 int Discount;
 int.TryParse(TB_Discount.Text, out Discount);
 TB_REMAINDER.Text = (Convert.ToInt32(TB_PAID_CASH.Text) - Convert.ToInt32(TB_TOTAL_INV.Text) - Discount).ToString();
 }
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
 }
}
Tolani
2,5017 gold badges31 silver badges49 bronze badges
asked Jul 22, 2016 at 21:39
\$\endgroup\$
5
  • \$\begingroup\$ If you are going to vote to close because of broken code, leave a comment. I have voted to leave this question open because the fault is non-obvious to me. \$\endgroup\$ Commented Jul 22, 2016 at 23:20
  • 1
    \$\begingroup\$ I also voted to leave open, but as written it seems that the OP is unclear as to whether or not the current method works. Perhaps instead of asking if this is the "correct way", which implies that something about it is wrong, ask if there is a "better", or "cleaner", or "faster", or "more robust" way. \$\endgroup\$ Commented Jul 23, 2016 at 0:41
  • 1
    \$\begingroup\$ If it is int then just bind to an int Public property and in the set set it to. On the binding on onpropertychanged notify. I did not vote to close but setting null to 0 is not in the code at all. \$\endgroup\$ Commented Jul 23, 2016 at 1:02
  • \$\begingroup\$ Is there a reason you're using TextBox rather than a NumericUpDown control for this? \$\endgroup\$ Commented Jul 26, 2016 at 17:40
  • \$\begingroup\$ @DanLyons yes, the TB_PAID_CASH textbox is entered by user and it could be 10 or 50000 and the TB_TOTAL_INV textbox is just to display the total for the user it cant be modified by users \$\endgroup\$ Commented Jul 27, 2016 at 11:55

1 Answer 1

5
\$\begingroup\$

You are mixing various things here. For some values you use the try/catch and for others the TryParse. I think you should stick to one of them.

What I also don't understand is why you are checking whether this is true string.IsNullOrEmpty(TB_Discount.Text) and then try to parse it anyway int.TryParse(TB_Discount.Text)?

Here is what you can do:

Create a property for each value:

public int TotalInv
{
 get
 {
 int totalInv;
 return int.TryParse(TB_TOTAL_INV.Text, out totalInv) ? totalInv : 0;
 }
}
public int PaidCash
{
 get
 {
 int paidCash;
 return int.TryParse(TB_PAID_CASH.Text, out paidCash) ? paidCash : 0;
 }
}
public int Discount
{
 get
 {
 int discount;
 return int.TryParse(TB_Discount.Text, out discount) ? discount : 0;
 }
}

For the reminder too:

public int Reminder
{
 get
 {
 return PaidCash - TotalInv - Discount;
 }
}

Then when the text changes you can update the text box with only one line:

private void TB_PAID_CASH_TextChanged(object sender, EventArgs e)
{
 if(PaidCash < TotalInv)
 {
 // todo: show message...
 }
 TB_REMAINDER.Text = Reminder.ToString();
}
answered Jul 23, 2016 at 5:16
\$\endgroup\$
2
  • \$\begingroup\$ Thanks alot for help I was not know about it.. I read about But can I check the property ? for example if PaidCash property are less than TotalInv property I want to show the user a message box to notify him ? \$\endgroup\$ Commented Jul 26, 2016 at 16:44
  • \$\begingroup\$ @samer sure, you can, I edited the code. See the if in the last example. \$\endgroup\$ Commented Jul 26, 2016 at 16:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.