Errors with typecasting
-
Hello, I am having problems with a tree control in a windows form application. Sometime yesterday a typecast that I was using wuit functioning and I can't figure out where the problem started. I have a class that is an extents the System.Windows.Forms.TreeNode class. It adds fine to the TreeControl, but when I go back and try to read the object and cast it as the new class it throws the following exception. What could have happened to make this start failing? I am including the adding and the locating a node. The adding program sometimes throws a duplicate value, which is tested by the FindChildNode before adding it. The FindChildNode is the biggest problem right now and is throwing the following Exception (Bolded line is throwing the error). Message="Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'InternalApp.ECDTreeNode'."
public class ECDTreeNode : System.Windows.Forms.TreeNode { . . . public ECDTreeNode FindChildNode(string cText) { ECDTreeNode ret = null; for (int i = 0; i < Nodes.Count; i++) { **ECDTreeNode tn = (ECDTreeNode)Nodes[i];** if (cText.Trim().ToLower().CompareTo(tn.Text.Trim().ToLower()) == 0) { ret = tn; break; } } return ret; } } public void AddSorted(ECDTreeNode tn) { bool bFound = false; try { for (int i = 0; i < Nodes.Count; i++) { if (tn.Text.CompareTo(Nodes[i].Text) < 0) { Nodes.Insert(i, tn.Text.Trim()); bFound = true; break; } } if (!bFound) Nodes.Add(tn); } catch (Exception e) { MessageBox.Show(e.Message, "Exception Error", MessageBoxButtons.Ok, MessageBoxIcon.Error); } } }
Thanks for any help,Leo T. Smith Program/Analyst Supervisor
-
Hello, I am having problems with a tree control in a windows form application. Sometime yesterday a typecast that I was using wuit functioning and I can't figure out where the problem started. I have a class that is an extents the System.Windows.Forms.TreeNode class. It adds fine to the TreeControl, but when I go back and try to read the object and cast it as the new class it throws the following exception. What could have happened to make this start failing? I am including the adding and the locating a node. The adding program sometimes throws a duplicate value, which is tested by the FindChildNode before adding it. The FindChildNode is the biggest problem right now and is throwing the following Exception (Bolded line is throwing the error). Message="Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'InternalApp.ECDTreeNode'."
public class ECDTreeNode : System.Windows.Forms.TreeNode { . . . public ECDTreeNode FindChildNode(string cText) { ECDTreeNode ret = null; for (int i = 0; i < Nodes.Count; i++) { **ECDTreeNode tn = (ECDTreeNode)Nodes[i];** if (cText.Trim().ToLower().CompareTo(tn.Text.Trim().ToLower()) == 0) { ret = tn; break; } } return ret; } } public void AddSorted(ECDTreeNode tn) { bool bFound = false; try { for (int i = 0; i < Nodes.Count; i++) { if (tn.Text.CompareTo(Nodes[i].Text) < 0) { Nodes.Insert(i, tn.Text.Trim()); bFound = true; break; } } if (!bFound) Nodes.Add(tn); } catch (Exception e) { MessageBox.Show(e.Message, "Exception Error", MessageBoxButtons.Ok, MessageBoxIcon.Error); } } }
Thanks for any help,Leo T. Smith Program/Analyst Supervisor
-
Leo Smith, What is the collection "Nodes" ?, Is it a collection of System.Windows.Forms.TreeNode or ECDTreeNode? Regards, Gareth.
-
My bet is that you are unintentionally adding a node somewhere using a string to the collection, instead of sending your pre-created node. Put a breakpoint in on the problem line to see what type the node is, and I bet it is just a TreeNode. Once you see which node is causing the problem, try to trace back to when it was created, and I bet you have a line like "tree.Nodes.Add("Some string")" instead of creating your own ECDTreeNode object, then adding that object. Good luck,
Sounds like somebody's got a case of the Mondays -Jeff
-
Leo Smith, If this was true, why do you need to cast it?, since Nodes[i] would return a ECDTreeNode. Think it might be a good idea to:
Trace.WriteLine("node == " + Nodes[i].ToString());
Regards, Gareth.
I think that Leo was saying that the objects are EXPECTED by him to be of his type... I don't think that the collection is actually strongly typed to his objects, though, which is what is leading to the error (I think). I also don't think that type casting an object to it's own type throws an exception, but I can't say that I have ever tried it to find out. Any comments on this, Leo? Is the collection strongly typed to your object, as understood by Gareth, or is it just a TreeNode collection, as interpreted by me?
Sounds like somebody's got a case of the Mondays -Jeff
-
Leo Smith, If this was true, why do you need to cast it?, since Nodes[i] would return a ECDTreeNode. Think it might be a good idea to:
Trace.WriteLine("node == " + Nodes[i].ToString());
Regards, Gareth.
Thanks both of you. The trace assisted in finding out that the was no error, since as Jeff suspected, I had added a string (although technically I inserted a string value and not a ECDTreeNode). I was unable to find this error only because I almost never insert a value. I had looked at all my Nodes.Add() calls before I posted. The trace let me know the type was wrong and Jeff's "you are probably adding a string" instead of a ECDTreeNode made me stop and look for something out of the ordinary. Thanks,
Leo T. Smith Program/Analyst Supervisor
-
Thanks both of you. The trace assisted in finding out that the was no error, since as Jeff suspected, I had added a string (although technically I inserted a string value and not a ECDTreeNode). I was unable to find this error only because I almost never insert a value. I had looked at all my Nodes.Add() calls before I posted. The trace let me know the type was wrong and Jeff's "you are probably adding a string" instead of a ECDTreeNode made me stop and look for something out of the ordinary. Thanks,
Leo T. Smith Program/Analyst Supervisor