0

I have two class with code like this (it's just example)

class parent1
   function f(param as integer) as integer
       return param +2
   end function
end class
'===============================
class parent2
  function f(param as integer) as integer
       return param *1
   end function
end class

then i want to create a class inherits of one of boot based on situation at run time. maybe like this:

class child
   sub new(s as string)
     'it will inherits parent1
   end sub
   sub new(i as integer)
     'it will inherits parent2
   end sub
end class

Is it possible..?

  • 1
    No. Inheritance is something that is done at compile time, not run time. There may be ways to accomplish what you want, but I'm afraid this isn't it. – Blackwood Nov 18 '16 at 00:15
  • Actually, we've done something like this. You can emit IL. See this http://www.drdobbs.com/generating-code-at-run-time-with-reflect/184416570 as an example – T.S. Nov 18 '16 at 02:04
  • Looks like an [XY problem](http://xyproblem.info/). Better explain what you actually want to accomplish. I guess [Interfaces](https://msdn.microsoft.com/en-us/library/28e2e18x.aspx) and/or [Delegates](https://msdn.microsoft.com/en-us/library/ms172879.aspx) will help you. – Ripple Nov 18 '16 at 02:32

2 Answers2

2

.NET languages do not support multi inheritance and you cannot change it during runtime as pointed out in all comments you got.
Instead, nice "workaround" was created for this problem called Interface. Somebody call it a "hack" solution :)

For me your problem is a problem for dependency injection.

You want have child class which can change behavior during runtime.
Create abstraction of behavior, you want to change, as interface

Public Interface IBehavior
    Function Calculate(value As Integer) As Integer
End Interface

Then create child class which takes behavior as constructor parameter

Public Class Child
    Private ReadOnly _behavior As IBehavior

    Public Sub New(behavior As IBehavior)
        _behavior = behavior
    End Sub

    Public Sub Execute(int value)
        Dim newValue As Integer = _behavior.Calculate(value)
        ' Do something else
    End Sub
End Class

Create implementations of IBehavior you want to use during runtime

Public Class SumBehavior Implements IBehavior
    Function Calculate(value As Integer) As Integer Implements IBehavior.Calculate
        Return value + 2
    End Function
End Class

Public Class MultiplyBehavior Implements IBehavior
    Function Calculate(value As Integer) As Integer Implements IBehavior.Calculate
        Return value * 2
    End Function
End Class

Then during runtime you can change behavior of Child instance based on passed parameter

Dim sum As New SumBehavior()
Dim child As New Child(sum)
child.Execute(23)

Your problem is good example of "Open-Close Principle".
- Your Child class is closed for modifications -> Do something else staff in Execute method stays unmodified
- Your child class is open for modifications -> Behavior logic can be changed without touching Child class

Fabio
  • 31,528
  • 4
  • 33
  • 72
0

Multiple inheritance is not possible in .Net. Now, to solve your problem

  1. Try Dynamic Source Code Generation and Compilation. You can not do multiple inheritance but during runtime when its sure which type of class has to be inherited, dynamically create the class using CodeDom. Refer msdn link
  2. This approach may or may not suit your need. Still if you have an scope to change the design, Use interfaces and dependency injection. (Since the function name is same in both the classes parent1 and parent2 you can take advantage of polymorphism).
Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
  • Yes.. thanks, I think codedom are solutions. Can you tell me how to replace existing class with class generated from codedom..? I'm ask at here but no one answer. http://stackoverflow.com/questions/40713182/vb-net-replace-existing-class-with-codedoms-generated-class – Great Reward Nov 22 '16 at 00:44