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 system

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

      Shared Sub main()
 Dim o As New c2()
         o.hi()
      End Sub

     'The sub which overrides the method of the base class must have the
keyword overrides
     Overrides Sub hi()
        console.writeline("New and Improved method hi")
     End Sub
   End Class


Source: DotNetExtreme.com

Dig Deeper on VB 6 to VB .NET Migration

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close