Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Errors with typecasting

Errors with typecasting

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Leo Smith
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • L Leo Smith

      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

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      Leo Smith, What is the collection "Nodes" ?, Is it a collection of System.Windows.Forms.TreeNode or ECDTreeNode? Regards, Gareth.

      L 1 Reply Last reply
      0
      • G Gareth H

        Leo Smith, What is the collection "Nodes" ?, Is it a collection of System.Windows.Forms.TreeNode or ECDTreeNode? Regards, Gareth.

        L Offline
        L Offline
        Leo Smith
        wrote on last edited by
        #3

        Nodes is a collection ECDTreeNodes.

        Leo T. Smith Program/Analyst Supervisor

        S G 2 Replies Last reply
        0
        • L Leo Smith

          Nodes is a collection ECDTreeNodes.

          Leo T. Smith Program/Analyst Supervisor

          S Offline
          S Offline
          Skippums
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • L Leo Smith

            Nodes is a collection ECDTreeNodes.

            Leo T. Smith Program/Analyst Supervisor

            G Offline
            G Offline
            Gareth H
            wrote on last edited by
            #5

            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.

            S L 2 Replies Last reply
            0
            • G Gareth H

              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.

              S Offline
              S Offline
              Skippums
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • G Gareth H

                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.

                L Offline
                L Offline
                Leo Smith
                wrote on last edited by
                #7

                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

                G 1 Reply Last reply
                0
                • L Leo Smith

                  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

                  G Offline
                  G Offline
                  Gareth H
                  wrote on last edited by
                  #8

                  Glad to be of some help. Regards, Gareth.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups