3

I'm a newbie to C#, but can't seem to find anything about this problem. Here's what I'm trying to do:

string testString = txtBox1.Text;
string testString2 = txtBox2.Text;

if ((testString == "") || (testString2 == ""))
{
    MessageBox.Show("You must enter a value into both boxes");
    return;
} 

Basically I need to check to see if either txtBox1 or txtBox2 is blank. However I'm getting an error when running this. What's the proper way to do this (or is my approach all wrong)?

6
  • 4
    What does "accept" mean? Commented Jul 1, 2012 at 16:35
  • sorry I have clarified the problem, my original question did not fully include everything. I pressed save too soon before I realized it did not cover the actual problem I was having. Commented Jul 1, 2012 at 16:40
  • 6
    What's the error? Commented Jul 1, 2012 at 16:40
  • 1
    In the code sample you provided, there is a closed parentheses ')' missing in the if. Commented Jul 1, 2012 at 16:40
  • @Tibi: the code looks fine to me. Commented Jul 2, 2012 at 3:36

6 Answers 6

7

Since you want to check whether textboxes contains any value or not your code should do the job. You should be more specific about the error you are having. You can also do:

if(textBox1.Text == string.Empty || textBox2.Text == string.Empty)
   {
    MessageBox.Show("You must enter a value into both boxes");
   }

EDIT 2: based on @JonSkeet comments:

Usage of string.Compare is not required as per OP's original unedited post. String.Equals should do the job if one wants to compare strings, and StringComparison may be used to ignore case for the comparison. string.Compare should be used for order comparison. Originally the question contain this comparison,

string testString = "This is a test";
string testString2 = "This is not a test";

if (testString == testString2)
{
    //do some stuff;
}

the if statement can be replaced with

if(testString.Equals(testString2))

or following to ignore case.

if(testString.Equals(testString2,StringComparison.InvariantCultureIgnoreCase)) 
Sign up to request clarification or add additional context in comments.

7 Comments

I would personally suggest using Equals for equality comparisons. That's what it's there for, after all.
@JonSkeet, I agree, I wasn't sure about the original unedited post, if the OP want to compare for equality or wants to compare the strings ignoring case,
Why on earth would case matter when the OP is comparing for the strings being empty? Even if you do want to do a case-insensitive comparison, you can still use Equals with a StringComparison, which is clearer than using Compare IMO. Compare should be used for an order comparison, not equality. I'm amazed this answer has been accepted, as I can't see how it helps the OP with the question as stated.
@JonSkeet, that was my miss understanding that the OP want's to compare strings, not the empty strings. Earlier there were two string variables with values and OP was comparing them using ==, later the edited post specified comparing against empty string. Even I am surprised at the answer being accepted. Probably due to edited part of comparing textBox1.Text == string.Empty, but again there are other answers using Equals which are much better
Can you mention the reason for using string.Empty rather than "", if there's any? I'm no good C# programmer (only Java/Scala expert) and the point of string.Empty existing at all is not clear to me.
|
3

Here's a more valid way which also check if your textbox is filled with only blanks.

// When spaces are not allowed
if (string.IsNullOrWhiteSpace(txtBox1.Text) || string.IsNullOrWhiteSpace(txtBox2.Text))
  //...give error...

// When spaces are allowed
if (string.IsNullOrEmpty(txtBox1.Text) || string.IsNullOrEmpty(txtBox2.Text))
  //...give error...

The edited answer of @Habib.OSU is also fine, this is just another approach.

3 Comments

in your comments you had the 'not' in the wrong place, if the method names make any sense. I assumed it was a typo and edited the answer, please correct it if the current one is actually wrong (but then please explain the weirdness).
@Blaisorblade No. In OP's code, he's throwing error message when string is equal to empty or contains white-space, so if string is empty, then throw the error or if string is white-space throw the error.
I see, sorry. I thought you implemented the intro. I've added comments to clarify the answer.
1

try

if (testString.Equals(testString2)){
}

2 Comments

This is correct, no clue why someone would downvote this other than being a duplicate but posted at the exact same time. Never mind the asholes buddy
The question is not about comparing the two strings with each other; the code should check if either is empty. The original question was different though and this was a sensible answer, so downvotes are probably not appropriate.
1

The code provided is correct, I don't see any reason why it wouldn't work. You could also try if (string1.Equals(string2)) as suggested.

To do if (something OR something else), use ||:

if (condition_1 || condition_2) { ... }

Comments

0

use if (testString.Equals(testString2)).

1 Comment

The question is not about comparing the two strings with each other; the code should check if either is empty. The original question was different though and this was a sensible answer, so downvotes are probably not appropriate.
0

Try:

    if (textBox1.Text == "" || textBox2.Text == "")
    {
        // do something..
    }

Instead of:

    if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
    {
        // do something..
    }

Because string.Empty is different than - "".

Comments

Your Answer

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.