Overrides Overridable methods : Overridable « Class Module « VB.Net Tutorial






Option Strict On
 Imports System
 Public Class Control

     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         Me.top = top
         Me.left = left
     End Sub

     Public Overridable Sub DrawControl( )
         Console.WriteLine("Control: drawing Control at {0}, {1}", top, left)
     End Sub

     Protected top As Integer
     Protected left As Integer
 End Class

 Public Class Label
     Inherits Control

     Public Sub New(ByVal top As Integer, ByVal left As Integer, ByVal n As String)
         MyBase.New(top, left)

         text = n
     End Sub 

     Public Overrides Sub DrawControl( )
         MyBase.DrawControl( ) 
         Console.WriteLine("Writing string to the listbox: {0}", text)
     End Sub 

     Private text As String 
 End Class 

 Public Class Button
     Inherits Control

     Public Sub New(ByVal top As Integer, ByVal left As Integer)
         MyBase.New(top, left)
     End Sub 'New

     Public Overrides Sub DrawControl( )
         Console.WriteLine("Drawing a button at {0}, {1}" + ControlChars.Lf, top, Left)
     End Sub 
 End Class 

 Public Class Tester

     Shared Sub Main( )
         Dim win As New Control(1, 2)
         Dim lb As New Label(3, 4, "test")
         Dim b As New Button(5, 6)
         win.DrawControl( )
         lb.DrawControl( )
         b.DrawControl( )
         
         Dim winArray(3) As Control
         winArray(0) = New Control(1, 2)
         winArray(1) = New Label(3, 4, "AAA")
         winArray(2) = New Button(5, 6)

         Dim i As Integer
         For i = 0 To 2
             winArray(i).DrawControl( )
         Next i
     End Sub
 End Class
Control: drawing Control at 1, 2
Control: drawing Control at 3, 4
Writing string to the listbox: test
Drawing a button at 5, 6

Control: drawing Control at 1, 2
Control: drawing Control at 3, 4
Writing string to the listbox: AAA
Drawing a button at 5, 6








6.42.Overridable
6.42.1.Overrides Overridable methods
6.42.2.Overrides and Shadows methods from base class
6.42.3.ToString method overrides
6.42.4.Derived Class and Overrides