-3

I have this code:

if (textBox1.Text == "one" || "two")

I have tried to use || and | to add more strings, but it says that it cannot be applied to operands of type "bool" and "string". How can I make this work? Thank you.

Sandro Milhano
  • 27
  • 1
  • 1
  • 6

4 Answers4

6

Try this

if (textBox1.Text == "one" || textBox1.Text == "two")
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
5

or alternatively :

var strings = new List<string>() {"one", "two", "thee", .... "n"};
if(strings.Contains(textBox1.Text)){
}
Tigran
  • 61,654
  • 8
  • 86
  • 123
5

You can't combine operators in the way I suspect you are trying:

if (textBox1.Text == "one" || "two")

You need to qualify each condition as follows:

if (textBox1.Text == "one" || textBox1.Text == "two")

There are ways to make this easier to do, see answers to this question for an alternative way to do it

Community
  • 1
  • 1
Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60
0

I'd recommend to use:

var options = new [] { "one", "two" };
if (options.Contain(textBox1.Text))
    ...
D.R.
  • 20,268
  • 21
  • 102
  • 205