c# - Unable to cast object type 'PictureBox' to myClassControl -


i have class extends control:

public foo : control  {   //..  } 

and control:

var baa = (((foo)((control)controls.find(controlname, true).first())); baa.etc = ..;  

but when do:

 ((picturebox)((control)controlimg)).myextensionmethod(..)  

i exception:

unable cast object of type 'system.windows.forms.picturebox' type 'controlextensions.foo'.

how fix exception , let me know.

thank you.

there no way fix this. class foo correct. error message explains all. foo not inherit picturebox. if foo picture box of sort, implement picturebox class , not control.

to give real life example:

interface ianimal { } class dog : ianimal { public static void bark() { } } class cat : ianimal { public static void meow() { } } 

the signature of cat different dog cat defines cat.meow(), whereas dog not. dog defines dog.bark(), whereas cat not. example, following code comments wrap head around this:

class program {     static void main(string[] args)     {         dog mydog = new dog(); // mydog contains definition bark         ianimal mypet = (ianimal)mydog; // cast not necessary.                                         // mypet's signiature of dog, typeof(mypet)                                         //   animal boxed (google on this)         cat mycat = (cat)mypet // try , imagine in real life                                // (turning dog cat) [i don't even]                                // doesn't work because cat doesn't                                //   contain definition bark()         cat mydimentedcat = (cat)(ianimal)mydog; // why code fails.     } } 


trying show same thing a square rectangle, rectangle isn't square:

interface irectangle { } interface isquare : irectangle { } 

Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -