Deriving or simulating deriving from multiple classes
In vb.net, I have a multiple user defined classes. One is named "actextbox" and inherits the windows forms textbox. Another is a user defined control named "opgTriState". I want all of these classes to have a property called "acControlSource" that simply Sets and Gets the value of a private string variable. All of the classes will have the same implementation for this property. Also, I want all of the classes to have a method called "acBindControl". In this case, each class needs to have its own implementation. My questions are:
1.) Is there a way to define the implementation (i.e. set and get) and associated variable for the "acControlSource" property only once for all these classes and somehow have each class inherit the property even though some of the classes are already derived from another class?
2.) How can I define an upper level class that I could CType to it in code to get intellisense for the "acBindControl" method but that would call the appropriate implementation for the specific (derived) class when invoked even though some of the classes are already derived from another class?
Here is my code so far so you get the idea of what I'm trying to accomplish (note: there will eventually be more classes. I'm trying to get rid of the case statement and put the specific data binding implementation within the classes:
Dim cntrlType As Type Dim strControlSource As String Dim propInfo As PropertyInfo For Each cntrl As Control In Me.Controls cntrlType = cntrl.GetType propInfo = cntrlType.GetProperty("acControlSource") If propInfo IsNot Nothing Then strControlSource = propInfo.GetValue(cntrl) If strControlSource <> "" Then Select Case cntrlType.Name Case "acTextBox" cntrl.DataBindings.Add("Text", das1, "Returns." & strControlSource) Case "opgTriState" cntrl.DataBindings.Add("opgValue", das1, "Returns." & strControlSource, True, DataSourceUpdateMode.Never, Nothing) AddHandler CType(cntrl, opgTriState).PropertyChanged, AddressOf WriteValueOPG End Select End If End If Next
Should acTextBox inherit system.Windows.Forms.TextBox, or Should acBaseControl inherit system.Windows.Forms.TextBox?
ste5an
AcBaseControl is just a way to avoid duplicate code. In the given sample it maybe not necessary, cause I don't know how complex your bind method is.
The inheritance and the casting is done to one of the interfaces. Either IAcControl, which requires more boilerplate code or IAcControlProperty, Both show in the above samples how you can use a common control object. Which shows how to use composition as replacement for multi-class inheritance.
Just play with the different aproaches. Look for solutions avoiding redundant code..