Click here to Skip to main content
15,896,448 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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...........


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

                }
            }
        }

    }
Posted
Comments
[no name] 11-May-14 13:55pm    
Do you really think that we are going to read through all of this to find some tiny section of code where your error is coming from?
Member 10740412 11-May-14 13:59pm    
the exception is coming at this point
if (((String)realtokens[j] == "dint" || (String)realtokens[j] == "dig" || (String)realtokens[j] == "chart") && ident((String)realtokens[j + 1]) == true && (String)realtokens[j + 2] == ":")
BillWoodruff 11-May-14 14:07pm    
Set a break-point on this line, and then when you hit the break-point, examine the values in 'realtokens[]. Determine how far the evaluation proceeds before the error is thrown.
Member 10740412 11-May-14 14:16pm    
evaluation does not proceed... it gives the exception
Unable to cast object of type 'System.Char' to type 'System.String'.
[no name] 11-May-14 14:10pm    
Possibly realtokens[j + 2] is a Char and not a string. You need to debug that line then and find out the data that is a char and not a 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.
C#
List<string> realtokens = new List<string>();
for (int i = 0; i < listBox6.Items.Count; i++)
{
    realtokens.Add(listBox6.Items[i].ToString());
}

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"
becomes
((realtokens[j] == "dint"
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] == ":")
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])
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
C#
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] == ")"))
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...
VB
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 ...
C#
private bool IsIn(string test, string[] against)
{
    if (against.Contains(test))
        return true;
    else
        return false;
}
then you can replace all those OR's (||) with a call to that function
&& IsIn(realtokens[h + 2], new string[]{"++","--","**","^/","/>","/<","</=",">/=","/=="}) 
 
Share this answer
 
v3
Comments
CHill60 12-May-14 8:40am    
I (somewhat foolishly!) wrote a Tip about simplifying the if statements ... A SQL-Like IN function for c# and VB.NET[^] ... I'm not advertising the Tip, but if you look at the comments posted against it you will find some (far) better ideas!
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.
 
Share this answer
 
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 .
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900