SiamIT
Well-known member
- Joined
- Aug 3, 2021
- Messages
- 53
- Programming Experience
- 5-10
Greetings,
I am in a need to extend a class to add few new methods/properties that base class lacks.
so, i have created a class that extend another class. but issue arises when i need to covert/cast base class to derived class. it doesn't works.
so? is i am doing it wrong, or it's not possible to cast directly? if so then what is the alternative way.. because i need the cast to have the new methods to base class.
this is just a sample code for testing (for this thread/post), actual class is little bigger/complex than this.. but the principle/issue is same..
so? how can i do this? please?
best regards
I am in a need to extend a class to add few new methods/properties that base class lacks.
so, i have created a class that extend another class. but issue arises when i need to covert/cast base class to derived class. it doesn't works.
so? is i am doing it wrong, or it's not possible to cast directly? if so then what is the alternative way.. because i need the cast to have the new methods to base class.
this is just a sample code for testing (for this thread/post), actual class is little bigger/complex than this.. but the principle/issue is same..
C#:
public class LabelEx : System.Windows.Forms.Label
{
public LabelEx TestCast()
{
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
//return label; //this doesn't even compile
return (LabelEx)label; //this complies but on execution it throws error
}
}
so? how can i do this? please?
best regards