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

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

问题描述

我正在开发一个自我指导的简单程序来练习我迄今为止学到的概念.我的项目与国际象棋有关,在这种情况下特别是棋盘(a-h 列和 1-8 行).用户被要求输入特定棋子的当前位置,希望输入为列的字母后跟行的数字.为了验证这一点,我首先检查这个值是否作为两个字符的字符串输入是有意义的,否则输入的内容已经不正确.然后,在将输入的字符串与可接受的数组元素列表进行比较之前,我将输入的字符串转换为小写字符.

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.

通过搜索这个网站,我的印象是一个字符串将其字符存储为数组并使用 char 属性的字符串,您将能够拉出第一个字符,从而将 char 与 char 进行比较.在我的搜索中,我还没有找到任何足够具体的东西,让我真正了解正在发生的事情.这个是我最接近的选项遇到过我认为不适用于本案的情况.任何见解将不胜感激.

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.

运算符‘==’不能应用于‘char’类型的操作数和‘字符串’

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)");
        }
    }

推荐答案

与 C 不同,字符串和字符数组是不同的.C# 中的字符串可以被视为一个字符数组,但您应该将它们视为不同的,因此=="比较是不合适的.看到这一点的一种简单方法是使用以下简单表达式

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!

看起来它应该可以工作,但它会产生与您看到的相同的错误,因为它试图将字符串a"与字符a"进行比较.在您的示例代码中,文本框控件的 Text 属性是字符串类型.

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]);

因为您没有将返回的值分配给变量,加上 'cl' 已经包含该特定值的小写.

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

这一行

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

不应产生错误,因为这两个项目都是 char 类型.

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

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

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