HGtez
HGtez

Reputation: 89

How to pass a vairable only if textbox is not empty C#

How can I assign a variable only if a textbox is not empty?

I am trying to get the text off the textboxes & add a letter to the prefix , and if they are not empty assign the text to a string variable and write them to a text file. If the textbox is empty then the variable should not be created, and omitted from the text file. This is an example the main string that is created with all the variables in it:

5012G65 P9811Y10C2F4H4Q4SU4VE2IT7W

The issue is that is the textbox is empty, the prefix letter is still outputted to the text file (bolded) (it should not appear at all if empty).

Thank you, in advance.

my code:

    if (radioButton1.Checked == true)
{

string X = "X" + textBox1.Text.ToString();
string C = "C" + textBox2.Text.ToString();
string F = "F" + textBox3.Text.ToString();
string H = "H" + textBox4.Text.ToString();
string Q = "Q" + textBox5.Text.ToString();
string S = "S" + textBox6.Text.ToString();
string U = "U" + textBox7.Text.ToString();
string V = "V" + textBox8.Text.ToString();
string E = "E" + textBox9.Text.ToString();
string I = "I" + textBox10.Text.ToString();
string T = "T" + textBox11.Text.ToString();
string W = "W" + textBox12.Text.ToString();

    string P9811A = string.Format("G65Y9811{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}", X, C, F, H, Q, S, U, V, E, I, T, W);

    string[] lines = {"5000O8011",
"5001(OSAMPLE)",
"5007(SINGLE)",
"5008(CHANGE ALL VALUES BEFORE RUNNING)",
"5012" + P9811A,
"5013M01",
"(CORRECT ALL )",

};
          System.IO.File.WriteAllLines(@"C:\WriteLines.txt", lines);

    }

Upvotes: 0

Views: 379

Answers (4)

Alexander Bell
Alexander Bell

Reputation: 7918

Use the String.IsNullOrEmpty() method (re: https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx), like the following:

if(!String.IsNullOrEmpty(textBox1.Text))...//your code her

Alternatively, starting with .NET 4 framework, you can use another method: String.IsNullOrWhiteSpace()

Hope this may help.

Upvotes: 0

Steve
Steve

Reputation: 216273

Use the Conditional Operator

 string X = string.IsNullOrWhiteSpace(textBox1.Text) ? "" : "X" + textBox1.Text;
 string C = string.IsNullOrWhiteSpace(textBox2.Text) ? "" : "C" + textBox2.Text;
 ......

and repeat for all other inputs

String.IsNullOrWhiteSpace is preferable because it removes the necessity to test for strings composed of all spaces or other characters like tab and newline and also you don't need to call Trim on these strings to remove them....

White-space characters are defined by the Unicode standard. The IsNullOrWhiteSpace method interprets any character that returns a value of true when it is passed to the Char.IsWhiteSpace method as a white-space character.

Upvotes: 0

saarrrr
saarrrr

Reputation: 2877

if (!String.IsNullOrWhiteSpace(textBox1.Text)) {
    X = "X" + textBox1.Text.ToString();
}

https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66439

Use a StringBuilder and append to it as needed.

Test whether each TextBox is empty before you add it to the StringBuilder.

var P9811A = new StringBuilder("G65Y9811");

if (textBox1.Text != "")
    P9811A.Append("X" + textBox1.Text);

if (textBox2.Text != "")
    P9811A.Append("C" + textBox2.Text);

// and so on...

When you need the value of the string you've been building, use P9811A.ToString().

Upvotes: 1

Related Questions