Polymorphism in VB

Why bother?

After ploughing through this tutorial, you might still be wondering what's the point? Well, hopefully, I will give you a few reasons below!

Polymorphism is important for performance reasons. For example, take a look at the following function:

Public Sub SaveToFile(ByVal sText As String, ByVal sFile As String, ByVal ConvertClass As Object)
   ' saves the results of the converter class to a file
   ' run conversion
   ConvertClass.Convert 'late bound
   ' Get a free file number
   nFileNum = FreeFile
   ' Create sFile
   Open sFile For Output As nFileNum
   ' Write the contents of ConvertClass
   Print #FileNum, ConvertClass.OutputText 'late bound
   ' Close the file
   Close FileNum
End Sub

The Convert method and OutputText property (in bold) are late bound to ConvertClass. This means that before your code is actually run, Visual Basic can't decide what kind of object a variable will contain. In this example, the ConvertClass argument is declared As Object, so at run time it could contain a reference to any kind of object — like a Car or a Rock.

Because it can't tell what the object will be, Visual Basic compiles some extra code to ask the object if it supports the method you've called. If the object supports the method, this extra code invokes it; if not, the extra code raises an error. Every method or property call incurs this additional overhead.

By using Interfaces, you can use early binding. Before your program is run, Visual Basic already knows what form the class is going to take (because you have told it that it is using x Interface - if it finds this isn't true, you will get a compile error!). When Visual Basic knows at compile time what interface is being called, it can check the type library to see if that interface supports the method. Visual Basic can then compile in a direct jump to the method, using a virtual function table (vtable). This is many times faster than late binding.

Now suppose the Convert method, and OutputText property to the ConvertClass interface, and that all Convert classes implement this interface. The ConvertClass argument can now be declared As IConvert, and the Convert method and OutputText property will be early bound:

Public Sub SaveToFile(ByVal sText As String, ByVal sFile As String, ByVal ConvertClass As IConvert)
   ' saves the results of the converter class to a file
   ' run conversion
   ConvertClass.Convert 'early bound (vtable)
   ' Get a free file number
   nFileNum = FreeFile
   ' Create sFile
   Open sFile For Output As nFileNum
   ' Write the contents of ConvertClass
   Print #FileNum, ConvertClass.OutputText 'early bound (vtable)
   ' Close the file
   Close FileNum
End Sub

This also means that as you type code, VB can actually use its autocomplete feature, listing all its properties and methods, making it easier for you to type code. It also means that you don't need to worry whether the current instance is cHTML2Text, or cText2HTML or anything else. This is especially beneficial when you have lots of different conversion classes, something that might not be so obvious in this tutorial, where we are only using two!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”