I usually use something like this for various reasons throughout an application:
if (String.IsNullOrEmpty(strFoo))
{
FooTextBox.Text = "0";
}
else
{
FooTextBox.Text = strFoo;
}
If I'm going to be using it a lot I will create a method that returns the desired string. For example:
public string NonBlankValueOf(string strTestString)
{
if (String.IsNullOrEmpty(strTestString))
return "0";
else
return strTestString;
}
and use it like:
FooTextBox.Text = NonBlankValueOf(strFoo);
I always wondered if there was something that was part of C# that would do this for me. Something that could be called like:
FooTextBox.Text = String.IsNullOrEmpty(strFoo,"0")
the second parameter being the returned value if String.IsNullOrEmpty(strFoo) == true
If not does anyone have any better approaches they use?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…