Home > Articles > Software Development & Management > Object Technology

  • Print
  • + Share This
From the author of

Polymorphism using Overloading

Function overloading is the creating of functions that have the same name yet different argument structures. The identically named functions behave differently according to the number, type, and order of the arguments that are passed to them.

The current version of Visual Basic allows you to imitate function overloading when using the Optional keyword or the Variant data type in the argument definition of a function. The following shows how to achieve function overloading using Variants and the Optional keyword as a Visual Basic work-around:

Public Function GetWidgets(WidgetType As Variant, _
                          Optional WidgetCount As Integer) As Variant
                    
    Dim l As Long
    Dim str As String
    Dim strBuffer As String
    Dim i As Integer
    
    'Behavior based on data type Long
    If TypeName(WidgetType) = "Long" Then
        If WidgetCount > 0 Then
            l = CLng(WidgetType) * WidgetCount
        Else
            l = CLng(WidgetType)
        End If
        GetWidgets = l
    End If
    
    
    'Behavior based on data type String
    If TypeName(WidgetType) = "String" Then
        str = "I am a really cool widget of type: " _
                   & CStr(WidgetType) & vbCrLf
        
        If WidgetCount > 0 Then
            For i = 1 To WidgetCount
                'Create a return line based on WidgetCount
                strBuffer = strBuffer & str
            Next
        Else
            strBuffer = str
        End If
        GetWidgets = strBuffer
    End If

The following shows how to achieve the same type of function overloading using Java code:

public class Widgets
{
    public long getWidgets(long widgetType){
        return widgetType;
    }
    
    public long getWidgets(long widgetType,
                           int widgetCount){
        return widgetType * widgetCount;
    }
    
    public String getWidgets(String widgetType){
        String str = new String();
        str = "I am a really cool widget of type: ";
        str = str + widgetType;
        return str;
    }
    
    public String getWidgets(String widgetType,
                             int widgetCount){
        String str = new String();
        String strBuffer = new String();
        
        str = "I am a really cool widget of type: ";
        str = str + widgetType + "\n";
        
        for(int i = 1; i <= widgetCount; i++){
            strBuffer = strBuffer  + str; 
        }
        return strBuffer;
    }
}

As you can see, the Visual Basic code requires that you do a lot of “logical fooling around” in order to achieve function overloading. This is a lot of work when compared to the above Java code, which allows the simplicity of defining each method definition as distinct even though the function names are the same. Presently, Visual Basic would never allow you to have two functions with the same name in the same class module, regardless of argument type and count.

Visual Basic 7 plans to address this shortcoming by introducing the Overloads keyword. The code snippet below shows how Microsoft plans to introduce the use of the Overloads keyword in VB 7 to overload functions:

Overloads Function GetWidgets(WidgetType as Long) as Long

Overloads Function GetWidgets(WidgetType as Long, WidgetCount as Long) as Long

Overloads Function GetWidgets(WidgetType as String) as String

Overloads Function GetWidgets(WidgetType as Long, _
                              WidgetCount as Long) as String
  • + Share This
  • 🔖 Save To Your Account