Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

后端 未结 5 1830
情深已故
情深已故 2020-12-07 01:40

I’m working on a self directed simple program to practice concepts I’ve learned thus far. My project is related to chess, in this case specifically the board (columns a-h an

相关标签:
5条回答
  • 2020-12-07 02:28

    Try to use this comparison:

    (cl.ToCharArray())[0] == gridColumns[i]
    
    0 讨论(0)
  • 2020-12-07 02:36

    Looping through the array just to see if an element is contained in it is kind of overkill when an array has a .Contains method. Something like this without the for loop should work:

        if (gridColumns.Contains(cl[0]))
        {
            //TODO
        } 
    
    0 讨论(0)
  • 2020-12-07 02:37

    I ran your program and it works fine. I think problem is some place else.

    enter image description here

    0 讨论(0)
  • 2020-12-07 02:39

    Unlike C, a string and an array of char are different. A string in C# can be viewed as an array of char but you should consider them different, therefore the '==' comparison isn't appropriate. One easy way to see this is with the following simple expression

       if ("a" == 'a') { /* do something */ } // ERROR!
    

    It looks like it should work but it generates the same error you are seeing, because it is trying to compare the string "a" to the char 'a'. In your example code the Text property of your textbox control is of type string.

    The string class has an indexer that allows you to treat a string as an array of char, but it's usually better (simpler) to use one of the many string methods to accomplish your goal. Consider this:

            var gridcolumns = "abcdefgh";
            var gridrows = "12345678";
            var input = "a1"; // column row
            var col = gridcolumns.IndexOf(input[0]); // 0 -7
            var row = gridrows.IndexOf(input[1]); // 0 -7
    

    In the code you gave I don't see a line that would generate the error you provided. The following line serves no purpose

               Char.ToLowerInvariant(currentLocationTextBox.Text[0]);
    

    Because you are not assigning the returned value to a variable, plus 'cl' already contains the lowercasing of that particular value.

    This line

                if (cl[0] == gridColumns[i])
    

    should not generate the error because both items are of type char.

    0 讨论(0)
  • Dweeberly's answer when applied... and shortened: Answer: Change the single quotes to double-quotes.

    My reasoning: Assume the following code:

    string IAmAString;
    // set string to anything
    IAmAString = "Q";
    if (IAmAString == 'Q')
    {
     // do something, but never gets here because "Q" is a string, and 'Q' is a char
     // Intellisense gives error on the if statement of 
     // 
     // "Operator '==' cannot be applied to operands of types 'string' and 'char'"
     //
     // because C# is a strongly typed language, and the '==' is not by 
     // default (in VS2012) overloaded to compare these two different types.
     // You are trying to compare a string with something that
     // is not-string, and C# is trying to let you know 
     // that that is not going to work.
    }
    

    It is fixed by changing the quotes to double-quotes. C# definitely seems to consider single quotes around a single character to be Char, and not string.

    just change the quotes within the if statement:

    IAmAString = "Q";
    if (IAmAString == "Q")
    {
     // does whatever is here within reason; "Q" is a string and "Q" is a string
    }
    

    At least this worked for me, and that's my 'quick' (where 'q' != "q") explanation of why.
    Getting back to work...

    0 讨论(0)
提交回复
热议问题