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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Error: Unable to cast object of type *** to type *** [modified]

Error: Unable to cast object of type *** to type *** [modified]

Scheduled Pinned Locked Moved C#
game-devhelpquestion
6 Posts 4 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.
  • A Offline
    A Offline
    Anthony Mushrow
    wrote on last edited by
    #1

    I put this in m code: public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; } And have this code ListViewItem item = new ListViewItem(new string[] {reader[NAME].ToString(), reader[GAME].ToString(), reader[BNAME].ToString(), reader[BOARD].ToString()}); Row row = (Row)item; <-- ERROR OCCURS HERE row.id = Convert.ToInt32(reader[ID].ToString()); row.on1ine = Convert.ToInt32(reader[ONLINE].ToString()); MainForm.listView1.Items.Add(row); But i get the above error, that it couldn't cast ListViewItem as Row. Ive done this before with TreeNode and it all worked fine, what wrong with this one? Why did my row.online get replaced with row.removed, it doesn't do it outside the code brackets? -- modified at 20:31 Tuesday 10th October, 2006

    A S 2 Replies Last reply
    0
    • A Anthony Mushrow

      I put this in m code: public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; } And have this code ListViewItem item = new ListViewItem(new string[] {reader[NAME].ToString(), reader[GAME].ToString(), reader[BNAME].ToString(), reader[BOARD].ToString()}); Row row = (Row)item; <-- ERROR OCCURS HERE row.id = Convert.ToInt32(reader[ID].ToString()); row.on1ine = Convert.ToInt32(reader[ONLINE].ToString()); MainForm.listView1.Items.Add(row); But i get the above error, that it couldn't cast ListViewItem as Row. Ive done this before with TreeNode and it all worked fine, what wrong with this one? Why did my row.online get replaced with row.removed, it doesn't do it outside the code brackets? -- modified at 20:31 Tuesday 10th October, 2006

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      I'm now just storing row in item.Tag, and it works (of course) but i still want to know why this won't work, i just can't see it.

      1 Reply Last reply
      0
      • A Anthony Mushrow

        I put this in m code: public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; } And have this code ListViewItem item = new ListViewItem(new string[] {reader[NAME].ToString(), reader[GAME].ToString(), reader[BNAME].ToString(), reader[BOARD].ToString()}); Row row = (Row)item; <-- ERROR OCCURS HERE row.id = Convert.ToInt32(reader[ID].ToString()); row.on1ine = Convert.ToInt32(reader[ONLINE].ToString()); MainForm.listView1.Items.Add(row); But i get the above error, that it couldn't cast ListViewItem as Row. Ive done this before with TreeNode and it all worked fine, what wrong with this one? Why did my row.online get replaced with row.removed, it doesn't do it outside the code brackets? -- modified at 20:31 Tuesday 10th October, 2006

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        Because ListViewItem and Row are different classes and don't have 1. an is-a relationship 2. implicit conversion operators. It obviously won't work with TreeNode also,as that class is not related in any way to the Row class that you wrote. You probably would have used the Tag property, just like you did now.

        Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

        A 1 Reply Last reply
        0
        • S S Senthil Kumar

          Because ListViewItem and Row are different classes and don't have 1. an is-a relationship 2. implicit conversion operators. It obviously won't work with TreeNode also,as that class is not related in any way to the Row class that you wrote. You probably would have used the Tag property, just like you did now.

          Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          Actually i didn't use the tag. Whith the tree node i had: public class Cheat : Windows.Forms.TreeNode { public string code; public string gameid; lots of other public strings } My class inherited from the TreeNode. So i had Cheat oNode = new Cheat(); oNode.code = stuff oNode.gameid = stuff this.treeview1.nodes.Add(oNode); and i could go: foreach(Cheat oNode in treeview1.selectednodes) and TreeNode main = new TreeNode(); Cheat gNode = (Cheat) main; or Cheat gNode = new Cheat(); TreeNode main = (TreeNode)gNode; Oh, and yes, when getting the nodes back from the list they still had all the values that where stored in my custom things by just going Cheat check = (Cheat)this.treeView1.SelectedNode; MessageBox.Show("The code in the node is... " + check.code); -- modified at 23:37 Tuesday 10th October, 2006

          M S 2 Replies Last reply
          0
          • A Anthony Mushrow

            Actually i didn't use the tag. Whith the tree node i had: public class Cheat : Windows.Forms.TreeNode { public string code; public string gameid; lots of other public strings } My class inherited from the TreeNode. So i had Cheat oNode = new Cheat(); oNode.code = stuff oNode.gameid = stuff this.treeview1.nodes.Add(oNode); and i could go: foreach(Cheat oNode in treeview1.selectednodes) and TreeNode main = new TreeNode(); Cheat gNode = (Cheat) main; or Cheat gNode = new Cheat(); TreeNode main = (TreeNode)gNode; Oh, and yes, when getting the nodes back from the list they still had all the values that where stored in my custom things by just going Cheat check = (Cheat)this.treeView1.SelectedNode; MessageBox.Show("The code in the node is... " + check.code); -- modified at 23:37 Tuesday 10th October, 2006

            M Offline
            M Offline
            monrobot13
            wrote on last edited by
            #5

            That code works because your Cheat class inherited from the TreeNode, which is exactly what you were casting it to and from. A TreeNode has no relation to a Row

            - Aaron

            1 Reply Last reply
            0
            • A Anthony Mushrow

              Actually i didn't use the tag. Whith the tree node i had: public class Cheat : Windows.Forms.TreeNode { public string code; public string gameid; lots of other public strings } My class inherited from the TreeNode. So i had Cheat oNode = new Cheat(); oNode.code = stuff oNode.gameid = stuff this.treeview1.nodes.Add(oNode); and i could go: foreach(Cheat oNode in treeview1.selectednodes) and TreeNode main = new TreeNode(); Cheat gNode = (Cheat) main; or Cheat gNode = new Cheat(); TreeNode main = (TreeNode)gNode; Oh, and yes, when getting the nodes back from the list they still had all the values that where stored in my custom things by just going Cheat check = (Cheat)this.treeView1.SelectedNode; MessageBox.Show("The code in the node is... " + check.code); -- modified at 23:37 Tuesday 10th October, 2006

              S Offline
              S Offline
              Stefan Troschuetz
              wrote on last edited by
              #6

              This works as you create a Cheat object, store its reference using a variable of its base class (TreeNode) and cast back later. Hereby the real object on the heap stays the same (Cheat object) all time long. The Row example doesn't work as you create an object of the base class (ListViewItem) and afterwards try casting to a sub class. This simply can't be done as the object on the heap is now Row.


              "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning." - Rick Cook

              www.troschuetz.de

              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