Results 1 to 16 of 16

Thread: Event handling?

Threaded View

  1. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Event handling?

    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:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim theInstance As A = New B
    5.         'This will show B's version of ShowValue.
    6.         theInstance.ShowValue()
    7.     End Sub
    8.  
    9. End Class
    10.  
    11. Public Class A
    12.  
    13.     Public Overridable Sub ShowValue()
    14.         MessageBox.Show("Class A")
    15.     End Sub
    16.  
    17. End Class
    18.  
    19. Public Class B
    20.     Inherits A
    21.  
    22.     Public Overrides Sub ShowValue()
    23.         MessageBox.Show("Class B")
    24.     End Sub
    25. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width