Error: Unable to cast object of type *** to type *** [modified]
-
I put this in m code:
public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; }
And have this codeListViewItem 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 -
I put this in m code:
public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; }
And have this codeListViewItem 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, 2006I'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.
-
I put this in m code:
public class Row : System.Windows.Forms.ListViewItem { public int id; public int online; }
And have this codeListViewItem 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, 2006Because
ListViewItem
andRow
are different classes and don't have 1. an is-a relationship 2. implicit conversion operators. It obviously won't work withTreeNode
also,as that class is not related in any way to theRow
class that you wrote. You probably would have used theTag
property, just like you did now.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
-
Because
ListViewItem
andRow
are different classes and don't have 1. an is-a relationship 2. implicit conversion operators. It obviously won't work withTreeNode
also,as that class is not related in any way to theRow
class that you wrote. You probably would have used theTag
property, just like you did now.Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro
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
-
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
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
-
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
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 nowRow
.
"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