本文介绍了无法将类型为“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  ");
                              }
                          }
                    }

                }
            }
        }

    }

推荐答案

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[]{"++","--","**","^/","/>","/<","</=",">/=","/=="}) 




这篇关于无法将类型为“System.Char”的对象强制转换为“System.String”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:28