Cast Problem
-
Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.
-
Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.
But that's very simple: at least one of the
Control
s ingroupBox.Controls
is not aWorkersCtrl
. TheControls
collection holdsControl
s or derived classes, not limited toWorkersCtrl
only. If you really only want to callDispose()
then changeWorkersCtrl c
intoControl c
and you'll be fine. mav -
But that's very simple: at least one of the
Control
s ingroupBox.Controls
is not aWorkersCtrl
. TheControls
collection holdsControl
s or derived classes, not limited toWorkersCtrl
only. If you really only want to callDispose()
then changeWorkersCtrl c
intoControl c
and you'll be fine. mavThanks, it was pretty stupid of me. I've got it working now.
-
Can someone tell me what is wrong with this code. I've got a couple of custom controls in a groupbox that i need to dispose. I tried the following foreach loop, but i get the error "Specified cast not valid". foreach (WorkersCtrl c in groupBox.Controls) { c.dispose(); } Thanks.
You must have a control inside the groupbox that is not of or inherit from WorkersCtl. You could try a couple of different things. You could either throw an if statement in there, to determine if the object is of type WorkersCtrl, and just use the object base class inside your for each decleration. Or...., you could change workersctrl to object, and set a debug statement within the foreach statement, outputing each objects .gettype().tostring() to determine which object is throwing the invalid cast error.