2

I am trying to create an if statement that will perform an action if any of the conditions are true. The items I need to test are the text values of eight textboxes. What I have right now is the following and the error it gives me says "Operator '||' cannot be applied to operands of type 'string' and 'string'".

if (textbox1.text = "" || textbox2.text = "" || ...... and so on)

If anyone knows a how I am supposed to be wording this if statement or an easy way to check to see if a .ini file at a known location exists. Either would work for me.

0

5 Answers 5

20

You need to use the equality operator == not the assignment =.

if (textbox1.text == "" || textbox2.text == "" || ...... and so on)

Your textbox1.text = "" sets the value of textbox1.text to an empty string, and returns this as the result of the assignment. You are therefore attempting to use a boolean or (||) on two strings.

Sign up to request clarification or add additional context in comments.

Comments

3

In C#, if you are checking if a value is equal - you need to use double equal sign.

Your code should look like this:

if (textbox1.text == "" || textbox2.text == "" || ...... and so on)

Comments

2

String.IsNullOrEmpty() will return a boolean for you:

http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.aspx

..and, like others have said, use "==" for testing equality.

Comments

1
textbox1.text == "" ||

Note the DOUBLE = sign.

as written, your question would evaluate by first assigning empty string to each variable then applying the OR to those, so by the time the boolean is evaluated it looks like

if ("" || "" || "" ... etc)

and the OR operator doesn't work on strings. Note that if you are not careful, then you'll quietly break this rule.

if (x = true || y = false) evaluates to if (true || false), and won't give you such a timely compiler warning

Comments

1

System.IO.File.Exists(path), returns a boolean

2 Comments

How is this in any way relevant to the question?
The last bit of the question: "...or an easy way to check to see if a .ini file at a known location exists."

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.