6

I'm taking a value from a textbox and converting it to decimal. But, the textbox value could be empty. So, how could I handle empty strings from the textbox?

Unfortunately I have around 50 textboxes to deal with, so answers like 'check for null with IF condition' won't help me. My code will look ugly if I use all those IF conditions.

I have this

Convert.ToDecimal(txtSample.Text)

To handle nulls, I did this

Convert.ToDecimal(txtSample.Text = string.IsNullOrEmpty(txtSample.Text) ? "0" : txtSample.Text)

But, the above code is displaying '0' in the textbox. User does not want to see '0'. Another solution is to take text box value into a variable and convert the variable like below.

string variable = txtSample.Text;
Convert.ToDecimal(variable = string.IsNullOrEmpty(variable) ? "0" : variable)

But again, I do not want to define around 50 variables. I am looking for some piece of code that handles null values during conversion without adding the extra line of code.

EKrueger
  • 730
  • 8
  • 20
ARB
  • 285
  • 1
  • 6
  • 17
  • What type of app is this? WPF? ASPNET? The reason I ask is becuase a WPF app has [Value Converters](http://wpftutorial.net/ValueConverters.html) that make this super easy. – Tacoman667 Jun 19 '14 at 14:42
  • +1'd the post to offset a spurious -1(?). Dunno why people still -1 posts without a reason... c'mon folks... – code4life Jun 20 '14 at 21:42

3 Answers3

17

But, the above code is displaying '0' in the textbox. User does not want to see '0'.

This is because your statement is assigning the new value to txtSample.Text (when you do txtSample.Text = ...). Just remove the assignment:

Convert.ToDecimal(string.IsNullOrEmpty(txtSample.Text) ? "0" : txtSample.Text)

To make things easier if you have many text fields to handle, you can define an extension method :

public static string ZeroIfEmpty(this string s)
{
    return string.IsNullOrEmpty(s) ? "0" : s;
}

And use it like this:

Convert.ToDecimal(txtSample.Text.ZeroIfEmpty())
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • lol..I am feeling like an idiot after reading your answer. haha. Why did I assign the value to the textbox in the first place? and when the problem was obvious why was I not thinking of removing the assignment? What the hell have I been thinking for 2 hours to fix this? haha. I think my programming part of brain was shut down and I was thinking with other parts of the brain. :) . Anyway, thanks mate. – ARB Jun 19 '14 at 14:50
2

You could make a function to keep from copying the code all over the place.

decimal GetTextboxValue(string textboxText)
{
    return Convert.ToDecimal(string.IsNullOrEmpty(textboxText) ? "0" : textboxText);
}

and then use it like this:

GetTextboxValue(txtSample.Text);
Raj Baral
  • 661
  • 6
  • 19
Becuzz
  • 6,846
  • 26
  • 39
0

You can create an extension method for the string as below

        public static decimal ToDecimal(this string strValue)
        {
            decimal d;
            if (decimal.TryParse(strValue, out d))
                return d;
            return 0;
        }

Then you can just txtSample.Text.ToDecimal() in every place.

Kiran Hegde
  • 3,651
  • 1
  • 16
  • 14