无法将类型为“System.Char”的对象强制转换为“System.String”。

问题描述:

无法将'System.Char'类型的对象强制转换为'System.String'类型。



我在编译器的类型检查阶段进行编码。我的任务...........

但是我遇到了这个例外而且不知道如何处理它........

有人可以帮助我....



这里是我的代码...........





Unable to cast object of type 'System.Char' to type 'System.String'.

i am coding on the type checking phase of compiler. for my assignment...........
but i am encountering this exception and do not know how to handle it........
can anyone please help me....

here is my code...........


//---------------------------- Type Checker-----------------------//
        private void button5_Click(object sender, EventArgs e)
        {
                ArrayList realtokens = new ArrayList();
                for (int i = 0; i < listBox6.Items.Count; i++)
                {
                    realtokens.Add(listBox6.Items[i]);
                }
                for (int j = 0; j < realtokens.Count; j++)
                {
                if (((String)realtokens[j] == "dint" || (String)realtokens[j] == "dig" || (String)realtokens[j] == "chart") && ident((String)realtokens[j + 1]) == true && (String)realtokens[j + 2] == ":")
                {
                    String a = (String)realtokens[j];
                    String b = (String)realtokens[j + 1];
                    fortype.Add(a);
                    fortype.Add(b);
                    for (int h = 0; h < realtokens.Count; h++)
                    {
                        if (ident((String)realtokens[h]) == true && (String)realtokens[h + 1] == "/=" && (integers((String)realtokens[h + 2]) == true || strings((String)realtokens[h + 2]) == true))
                        {
                            if ((idtypecheck((String)realtokens[h]) == "chart") && integers((String)realtokens[h + 2]) == true)
                            {
                                MessageBox.Show("string Identifier is assigned a numeric value");
                            }
                            else if ((idtypecheck((String)realtokens[h]) == "dig") && strings((String)realtokens[h + 2]) == true)
                            {
                                MessageBox.Show("numeric Identifier is assigned a string value");
                            }
                        }
                        else if (ident((String)realtokens[h]) == true && ((String)realtokens[h + 1] == "%" || (String)realtokens[h + 1] == "--" || (String)realtokens[h + 1] == "++" || (String)realtokens[h + 1] == "**" || (String)realtokens[h + 1] == "^/") && ident((String)realtokens[h + 2]) == true)
                        {
                            if ((idtypecheck((String)realtokens[h]) == "chart") && (idtypecheck((String)realtokens[h + 2]) == "dig"))
                            {
                                MessageBox.Show("modulus operator occur between non numeric identifiers ");
                            }
                            else if ((idtypecheck((String)realtokens[h]) == "dig") && (idtypecheck((String)realtokens[h + 2]) == "chart"))
                            {
                                MessageBox.Show("modulus operator occur between non numeric identifiers ");
                            }

                        }
                        //else if ((String)realtokens[h] == "COLLECTION"  && recog_id((String)realtokens[h + 1]) == true && (String)realtokens[h + 2] == "[" && ((recog_id((String)realtokens[h + 3]) == true) || (recog_Num((String)realtokens[h + 3]) == true) || (recog_string((String)realtokens[h + 3]) == true)) && (String)realtokens[h + 4] == "]" && ((String)realtokens[h + 5] == "NUM" || (String)realtokens[h + 5] == "ALPHA" || (String)realtokens[h + 5] == "POINT")&& (String)realtokens[h + 6] == ";")
                        else if (ident((String)realtokens[h]) == true && (String)realtokens[h + 1] == "{" && (ident((String)realtokens[h + 2]) == true) && (String)realtokens[h + 3] == "}")
                        {
                            if ((idtypecheck((String)realtokens[h + 2]) == "chart") || (idtypecheck((String)realtokens[h + 2]) == "dint"))
                            {
                                MessageBox.Show("array index type is not integer ");

                            }
                        }
                        else if (ident((String)realtokens[h]) == true && (String)realtokens[h + 1] == "{" && ((ident((String)realtokens[h + 2]) == true) || (integers((String)realtokens[h + 2]) == true)) && (String)realtokens[h + 3] == "}" && (String)realtokens[h + 4] == "/=" && (ident((String)realtokens[h + 5]) == true || integers((String)realtokens[h + 5]) == true || strings((String)realtokens[h + 5]) == true) && (String)realtokens[h + 6] == ":")
                        {
                            if (idtypecheck((String)realtokens[h]) == "chart" && (idtypecheck((String)realtokens[h + 5]) == "dig"))//|| (recog_Num((String)realtokens[h + 5])==true) )
                            {
                                MessageBox.Show("type of assigned value(int) is not same as type of array identifier(string)  ");

                            }
                            else if (idtypecheck((String)realtokens[h]) == "dig" && ((idtypecheck((String)realtokens[h + 5]) == "chart") || (strings((String)realtokens[h + 5]) == true)))
                            {
                                MessageBox.Show("type of assigned value(String) is not same as type of array identifier(int)  ");

                            }

                        }
                        else if ((ident((String)realtokens[h]) == true || integers((String)realtokens[h]) == true) && ((String)realtokens[h + 1] == "<" || (String)realtokens[h + 1] == ">"))
                        {
                            if (idtypecheck((String)realtokens[h]) == "chart" || idtypecheck((String)realtokens[h]) == "dint" || integers((String)realtokens[h]) == true)
                            {
                                MessageBox.Show("increment / decrement operator is used with non-numeric identifier ");
                            }
                        }

                        else if ((String)realtokens[h] == "(" && (ident((String)realtokens[h + 1]) == true) && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/" || (String)realtokens[h + 2] == "/>" || (String)realtokens[h + 2] == "/<" || (String)realtokens[h + 2] == "</=" || (String)realtokens[h + 2] == ">/=" || (String)realtokens[h + 2] == "/=="  && (ident((String)realtokens[h + 3]) == true) && (String)realtokens[h + 4] == ")"))
                        {
                            if (idtypecheck((String)realtokens[h + 1]) == "chart" && idtypecheck((String)realtokens[h + 3]) == "chart" && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/"))
                            {
                                MessageBox.Show("condition type is other than Boolean  ");
                            }
                            else if (idtypecheck((String)realtokens[h + 1]) == "dig" && idtypecheck((String)realtokens[h + 3]) == "dig" && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/"))
                            {
                                    MessageBox.Show("condition type is other than Boolean  ");
                             }
                             else if (idtypecheck((String)realtokens[h + 1]) == "dint" && idtypecheck((String)realtokens[h + 3]) == "dint" && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/"))
                              {
                                        MessageBox.Show("condition type is other than Boolean  ");
                              }
                          }
                    }

                }
            }
        }

    }

进一步解决方案1 ​​...



1.您使用的是 ArrayList 来存储realtokens。 ArrayLists不是强类型的,即你可以在其中混合使用各种类型。从.NET 2.n起,你应该只使用ArrayList,如果你必须(例如链接到一些旧的API)。



考虑使用列出< string>< / string> 而不是例如

Further to Solution 1...

1. You are using an ArrayList to store "realtokens". ArrayLists are not strongly typed, i.e. you can have a mixture of types in there. Since .NET 2.n you should only use ArrayList if you absolutely have to (link to some old API for example).

Consider using List<string></string> instead e.g.
List<string> realtokens = new List<string>();
for (int i = 0; i < listBox6.Items.Count; i++)
{
    realtokens.Add(listBox6.Items[i].ToString());
}



你可能会在之前抛出异常,更容易发现错误。



2.这也意味着你不必完成所有的演员表,例如...


You might get the exception thrown earlier where it's easier to spot what's wrong.

2. It also means that you don't have to do all of that casting, for example ...

(((String)realtokens[j] == "dint"

变为

((realtokens[j] == "dint"

并且所有代码都变得更容易阅读 - 并且效率更高。



3.现在让我们看看行

and all of your code becomes easier to read - and more efficient.

3. Now let's look at the line

if (((String)realtokens[j] == "dint" || (String)realtokens[j] == "dig" || (String)realtokens[j] == "chart") && ident((String)realtokens[j + 1]) == true && (String)realtokens[j + 2] == ":")

这显然导致问题......除了现在会更简单。

which is apparently causing the problem... except it is going to be simpler now.

if ((realtokens[j] == "dint" || realtokens[j] == "dig" || realtokens[j] == "chart") && ident(realtokens[j + 1]) == true && realtokens[j + 2] == ":")

事实上因为我们强制列表全部是 string 错误可能已经消失了。



当你在调试时,不要只是尝试F10超过线路,用F11挖掘 - 或使用立即Visual Studio中的窗口打印出各种值。这样你就可以深入到失败的地步......例如将此类型输入到即时窗口

In fact because we have forced the list to be all string the error may have already gone away.

When you are debugging, don't just try to F10 over the line, dig into it with F11 - or use the Immediate window in Visual Studio to print the various values out. That way you can drill down to the point of failure ... e.g. type this into the immediate window

? ident(realtokens[j + 1])

并点击回车。如果你没有收到错误,那就拉出违规行的另一部分。



4.最后的礼物。

该行

and hit enter. If you don't get an error then pull out another part of the offending line.

4. A final gift.
That line

else if ((String)realtokens[h] == "(" && (ident((String)realtokens[h + 1]) == true) && ((String)realtokens[h + 2] == "++" || (String)realtokens[h + 2] == "--" || (String)realtokens[h + 2] == "**" || (String)realtokens[h + 2] == "^/" || (String)realtokens[h + 2] == "/>" || (String)realtokens[h + 2] == "/<" || (String)realtokens[h + 2] == "</=" || (String)realtokens[h + 2] == ">/=" || (String)realtokens[h + 2] == "/=="  && (ident((String)realtokens[h + 3]) == true) && (String)realtokens[h + 4] == ")"))

非常糟糕,无法阅读。我们刚刚摆脱了所有的转换,但也摆脱了实际上不需要的任何括号(它们只是混淆了事情)。

还考虑使用空格来使事情更清晰,丢弃 == true (这不是必需的)。整个事情看起来像这样...

is truly awful and impossible to read. We've just got rid of all of the casting, but also get rid of any brackets that aren't actually required (they just confuse matters).
Also consider using whitespace to make things clearer, and dropping == true (it's not required). The whole thing then looks like this...

else if (realtokens[h] == "("
    && ident(realtokens[h + 1]) 
    && (realtokens[h + 2] == "++" || realtokens[h + 2] == "--" || realtokens[h + 2] == "**" || realtokens[h + 2] == "^/" || realtokens[h + 2] == "/>" || realtokens[h + 2] == "/<" || realtokens[h + 2] == "</=" || realtokens[h + 2] == ">/=" || realtokens[h + 2] == "/==" 
    && (ident(realtokens[h + 3]) == true)
    && realtokens[h + 4] == ")"))



嗯......好一点但仍有改进的余地。



创建这样的函数...


Hm... a little better but still room for improvement.

Create a function something like this ...

private bool IsIn(string test, string[] against)
{
    if (against.Contains(test))
        return true;
    else
        return false;
}

然后你可以通过调用该函数替换所有这些OR(||)

then you can replace all those OR's (||) with a call to that function

&& IsIn(realtokens[h + 2], new string[]{"++","--","**","^/","/>","/<","</=",">/=","/=="}) 


调试和逐步执行时,可以轻松跟踪此类错误源代码。

您将得到错误的确切行。
This kind of error can be easily traced when you debug and step through your source code.
You will get the exact line where you get the error.


您的代码太长而无法读取。但您的问题的简短回答是使用Convert.ToString(passchartypevalue)转换为字符串。看到类似的问题 C#数据类型面试问题 [ ^ ]此处。
your code is too lengthy to read . But short answer for your question is use Convert.ToString(passchartypevalue) to convert to string . see similar questions C# data types interview questions [^] here .