运算符'=='不能应用于类型为“字符”和“串”的操作数 [英] Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’

查看:211
本文介绍了运算符'=='不能应用于类型为“字符”和“串”的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个自我定向简单的程序来实践概念迄今为止我所学到的。我的项目涉及国际象棋,在这种情况下,具体地,板(A-H和行1-8列)。要求用户为希望输入作为列后跟一个数字的行的信的特定棋子的当前位置。
为了验证这一点,对我有意义首先检查该值输入为两个字符的字符串,否则什么输入已经是不正确。
然后,我转换的输入的字符串它与我接受的数组元素的列表进行比较之前小写字符。

从搜索此<一个href=\"http://stackoverflow.com/questions/7922890/in-c-parse-string-into-individual-characters\">site我得到的IM pression一个字符串存储其字符数组,并使用的字符串的字符属性,你将能够拉断从而比较CHAR到char的第一个字符。我还没有发现我的搜索不够具体什么真的给我发生了什么事有很好的理解。 <一href=\"http://stackoverflow.com/questions/19033642/operator-cannot-be-applied-to-operands-of-type-char-and-string\">This是最接近的选项,我遇到,我并不觉得是适用于这种情况。任何有识之士将AP preciated。

这是如下code产生以下错误。


  

运营商'=='不能应用于类型为字符的操作数,
  字符串


 私人的char [] gridColumns =新的char [] {'A','B','C','D','E','F','G' , 'H', };    私人无效createMoveButton_Click(对象发件人,RoutedEventArgs E)
    {
        //分配文本框中输入相关的私有字段
        this.gameId = this.gameIdTextBox.Text;
        this.playerId = this.playerIdTextBox.Text;
        this.gamePiece = this.gamePieceTextBox.Text;
        this.currentLocation = this.currentLocationTextBox.Text;
        this.targetLocation = this.targetLocationTextBox.Text;        //当前位置应该永远只能是2个字符,从一开始,这是真正的保证。
        如果(currentLocationTextBox.Text.Length == 2)
        {
            //转换currentLocationTextBox的内容,以小写字符进行比较。
            串CL = currentLocation.ToLowerInvariant();            //通过我可能选择栏遍历数组
            的for(int i = 0; I&LT; gridColumns.Length;我++)
            {
                Char.ToLowerInvariant(currentLocationTextBox.Text [0]);
                //试图我的字符串的第一个字符比较我阵的炭元素。
                如果(CL [0] == gridColumns [I])
                {
                    //去做
                }
            }
        }
        其他
        {
            MessageBox.Show(检查你的起始位置,它需要一个小写字符变量(A-H)后跟一个数字(1-8)。);
        }
    }


解决方案

与C,一个字符串和字符数组是不同的。在C#中的字符串可以被看作是一个字符数组,但你应该考虑他们不同,因此'=='比较是不恰当的。一个简单的方法,看看这是用下面简单的前pression

 如果(A=='A'){/ *做某事* /} //错误!

它看起来像它应该工作,但它会产生你看到了同样的错误,因为它试图比较字符串a的字符A。在您的例子code你的文本框控件的Text属性是字符串类型。

String类有一个索引器,可以让你把一个字符串作为字符数组,但它通常是更好(简单)使用很多字符串方法之一来完成自己的目标。试想一下:

  VAR gridcolumns =ABCDEFGH
        VAR gridrows =12345678;
        无功输入=A1; //列排
        VAR COL = gridcolumns.IndexOf(输入[0]); // 0 -7
        VAR行= gridrows.IndexOf(输入[1]); // 0 -7

在你给我没有看到一条线,会产生你所提供的错误code。下面的代码行没有任何意义

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

由于您没有指派返回值给一个变量,再加上CL已经包含了特定值的lowercasing。

这行

 如果(CL [0] == gridColumns [I])

由于这两个项目是char类型不应该产生的错误。

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 and rows 1-8). The user is asked for the current location of a specific chess piece hopefully entered as a letter for the column followed by a number for the row. To validate this it made sense to me to first check if this value was entered as a string of two characters, otherwise what is entered is already incorrect. I then converted the entered string to lower case characters before comparing it with my list of acceptable array elements.

From searching this site I get the impression that a string stores its characters as an array and using the char property of string you would be able to pull off the first character thus comparing char to char. I have not yet found anything specific enough in my searches to really give me a good understanding of what is happening. This is the closest option I’ve come across which I didn’t feel was applicable to this case. Any insight would be appreciated.

The code that follows produces the following error.

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

    private char[] gridColumns = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', };

    private void createMoveButton_Click(object sender, RoutedEventArgs e)
    {
        // Assigns text box input to associated private fields
        this.gameId = this.gameIdTextBox.Text;
        this.playerId = this.playerIdTextBox.Text;
        this.gamePiece = this.gamePieceTextBox.Text;
        this.currentLocation = this.currentLocationTextBox.Text;
        this.targetLocation = this.targetLocationTextBox.Text;

        // Current location should only ever be 2 characters, ensure from the start this is true.
        if (currentLocationTextBox.Text.Length == 2)
        {
            // Converts contents of currentLocationTextBox to lower case characters for comparison.
            string cl = currentLocation.ToLowerInvariant();

            // Iterates through my array of possible column choices
            for (int i = 0; i < gridColumns.Length; i++)
            {
                Char.ToLowerInvariant(currentLocationTextBox.Text[0]);
                // Trying to compare the first character of my string to the char element of my array.
                if (cl[0] == gridColumns[i])
                {
                    //TODO
                }
            }
        }
        else
        {
            MessageBox.Show("Check your starting location. It needs to be a lower case character variable (a-h) followed by a number (1-8)");
        }
    }

解决方案

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.

这篇关于运算符'=='不能应用于类型为“字符”和“串”的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆