Problem solve Get help with specific problems with your technologies, process and projects.

Overriding in VB.NET

Write an improved version of the method of the base class without affecting the message passed by the client programs

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!


Method overriding gives a programmer the ability to write an improved version of the method of the base class without affecting the message passed by the client programs, which uses the derived class. The ability to override has been included in VB.NET.

.NET also provides a robust mechanism of overriding by enforcing certain checks, like including the keywords overridable in front of the methods. These can be overrided, and overrides in front of the method, which overrides the method of the base class. This prevents an accidental overriding of methods of the base class, a problem that is prominent in huge projects where the inheritance hierarchy is deep.

 
Imports</font color> system

    Class c1</font color>
      'The method to be overridden must be declared by the keyword Overridable
      Overridable Sub</font color> hi()
         console.writeline("Old Method hi")
      End Sub
    End Class</font color>
 
    Class</font color> c2
      Inherits</font color> c1

      Shared Sub</font color> main() Dim</font color> o As New</font color> c2() o.hi() End Sub</font color> 'The sub which overrides the method of the base class must have the keyword overrides Overrides Sub</font color> hi() console.writeline("New and Improved method hi") End Sub End Class</font color> 

Source: DotNetExtreme.com

Dig Deeper on VB 6 to VB .NET Migration

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close