VB.Net Tutorial/Class Module/Overridable
Overrides and Shadows methods from base class
<source lang="vbnet">Option Strict On
Imports System Imports Microsoft.VisualBasic Interface Printable Sub Read( ) Sub Write( ) End Interface Public Class Document : Implements Printable Public Sub New(ByVal s As String) Console.WriteLine("Creating document with: {0}", s) End Sub Public Overridable Sub Read( ) Implements Printable.Read Console.WriteLine("Document Virtual Read Method for Printable") End Sub Public Sub Write( ) Implements Printable.Write Console.WriteLine("Document Write Method for Printable") End Sub End Class Public Class Note : Inherits Document Public Sub New(ByVal s As String) MyBase.New(s) Console.WriteLine("Creating note with: {0}", s) End Sub Public Overrides Sub Read( ) Console.WriteLine("Overriding the Read method for Note!") End Sub Public Shadows Sub Write( ) Console.WriteLine("Implementing the Write method for Note!") End Sub End Class Class Tester Public Shared Sub Main( ) Dim theNote As Document = New Note("Test Note") If TypeOf theNote Is Printable Then Dim isNote As Printable = theNote isNote.Read( ) isNote.Write( ) End If Console.WriteLine(vbCrLf) theNote.Read( ) theNote.Write( ) Console.WriteLine(vbCrLf) Dim note2 As New Note("Second Test") If TypeOf note2 Is Printable Then Dim isNote2 As Printable = note2 isNote2.Read( ) isNote2.Write( ) End If Console.WriteLine(vbCrLf) note2.Read( ) note2.Write( ) End Sub End Class</source>
Creating document with: Test Note Creating note with: Test Note Overriding the Read method for Note! Document Write Method for Printable Overriding the Read method for Note! Document Write Method for Printable Creating document with: Second Test Creating note with: Second Test Overriding the Read method for Note! Document Write Method for Printable Overriding the Read method for Note! Implementing the Write method for Note!
Overrides Overridable methods
<source lang="vbnet">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</source>
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
ToString method overrides
<source lang="vbnet">Module Module1
Class Phone Public Number As String Public Sub New(ByVal Number As String) Me.Number = Number End Sub Public Overrides Function ToString() As String ToString = Number End Function End Class Sub Main() Dim MyPhone As New Phone("555-1212") Console.WriteLine(MyPhone) Console.WriteLine(MyPhone.ToString()) End Sub
End Module</source>
555-1212 555-1212