.net - Multiple inhertinace -
i have classes having .net control:
class mytextbox : inherits mycontrol control textbox
i use inheritance instead of having variable containing .net control:
class mytextbox : inherits textbox
since multiple inheritance not supported in .net, there way me inherit mycontrol class , having implementation of .net textbox?
class mycontrol : inherits control class mytextbox : inherits mycontrol (all methods, property, events of textbox class)
so can have attributes/operations of textboxes, comboboxes , whatever in subclasses plus personal 1 , generic atts/ops on mycontrol class inherited subones.
as said, .net (and modern languages , oop environments) won't support multi-inheritance - , design decision -.
not workaround actual solution wrapping.
if want control mycontrol
, textbox
there're other ways of getting such relationship: interfaces.
your specialized textbox
should inherit .net built-in textbox
control class , implement imycontrol
interface.
the functionality specializedtextbox
must implement can implemented in separate class , use members in implementation of imycontrol
on specializedtextbox
.
i'm going write down sample in c#, should enough you're vb.net developer , solution language-neutral:
public interface imycontrol { void do(); } public class specializedtextbox : textbox, imycontrol { private readonly mycontrolextension _extension = new mycontrolextension(this); public mycontrolextension extension { { return _extension; } } public void do() { extension.do(); } } public class mycontrolextension { public mycontrolextension(control wrappedcontrol) { _wrappedcontrol = wrappedcontrol; } private readonly control _wrappedcontrol; public control wrappedcontrol { { return _wrapedcontrol; } } public void do() { // stuff wrappedcontrol property contained control } }
now specializedtextbox
control have both imycontrol
, textbox
members.
as far know, enough simulate multi-inheritance, class can implement 1 or more interfaces.
Comments
Post a Comment