Dnereb, I don’t want to mess up this thread by putting stuff here out of context but I think it will clear some confusion about polymorphism. Polymorphism has nothing to do if the class implements interface or not: here is a polymorphism example, consider class A and class B. Class B derives from Class A and class A has a method called ShowValue. Class B overrides the ShowValue of its base class. I declare a variable “theInstance” of Type A and assign it an instance of class B. When I call “theInstance’s” ShowValue I call the class B’s version of the method. This is called polymorphism since the variable I declared is a type of A but since it holds an instance of B it figures out how to call the correct version of ShowValue method that is the B’s version.
And NO the problem should not be solved using interfaces. There is no need for interface. That is unnecessary and the wrong way of doing it. And what happens with inheritance? Why you need to implement an interface in order to have the same methods/events? The inheritance is just for that. I explained in my previous post what is interface for and not going to do in here too.
Here is a polymorphism example:
vb Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim theInstance As A = New B 'This will show B's version of ShowValue. theInstance.ShowValue() End Sub End Class Public Class A Public Overridable Sub ShowValue() MessageBox.Show("Class A") End Sub End Class Public Class B Inherits A Public Overrides Sub ShowValue() MessageBox.Show("Class B") End Sub End Class




Reply With Quote