VBA Classes - Functions overload

saboh12617

Board Regular
Joined
May 31, 2024
Messages
67
Office Version
  1. 365
Platform
  1. Windows
Hello,

According to my research, what I'm trying to do is not possible, but I've noticed that many people here have a more advanced knowledge of VBA than I do, so I'm relying on you.

1. I know that unfortunately VBA is rather strange in terms of its OOP function, but on the subject of classes, can we redefine some basic operators for our objects? I'm thinking in particular of “=” or printing to String.

2. Otherwise, how do you deal with overloading in general, at the level of functions with identical names but taking different arguments? Is the only way to define the arguments as Variants and then check the TypeName?

Thanks for your advice.

Example

VBA Code:
Sub mySub()
  Dim myClassInstance As New MyClass
  Dim str As String

  display myClassInstance
  display str
End Sub

Function display(var As Variant)
  Select Case TypeName(var)
  Case “MyClass”
    Debug.Print var.toStr
  Case “String”
    Debug.Print var
  Case Else
    '
  End Select
End Function
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Welcome to the forum.

There is no such thing as function overloading in VBx. AFAIK, the only way to achieve a similar functionality is what you did. ie: To define the function param(s) as Variant and then act accordingly, based on the variable type of the passed argument(s)

Using Optional parameter(s) and/or the ParamArray keyword can, in some scenarios and to a certain extent, make functions somewhat fexible and hence mimic function overloading. In other words, it can make a function run *different* code without doing a var type check of the passed arguments.

VBA Code:
Function Opt(Optional bBool As Boolean) As Boolean
    Opt = bBool
End Function

Sub Test()
    Debug.Print Opt        '<== Ret= False
    Debug.Print Opt(True)  '<== Ret= True
End Sub

VBA Code:
Function Sum(ParamArray Numbers() As Variant) As Double
    Dim i As Integer
    For i = LBound(Numbers) To UBound(Numbers)
        Sum = Sum + Numbers(i)
    Next i
End Function

Sub Test()
    Debug.Print Sum(10)                     '<== Ret= 10
    Debug.Print Sum(1, 1.5, 2, 100, -5, 8)  '<== Ret= 107.5
    Debug.Print Sum(2, 2)                   '<== Ret= 4
End Sub


When working with classes, it is *technically* possible to run diff code using functions with identical names if the function is defined in an interface class.

Interface class IPet
VBA Code:
Public Sub Sound()
    '
End Sub

Class C_Cat
VBA Code:
Implements IPet
Private Sub IPet_Sound()
    Debug.Print "meowww"
End Sub

Class C_Dog:
VBA Code:
Implements IPet
Private Sub IPet_Sound()
    Debug.Print "bark bark"
End Sub

Usage:
VBA Code:
Sub Test()

    Dim oCat As IPet, oDog As IPet
 
    Set oCat = New C_Cat
    Set oDog = New C_Dog
 
    oCat.Sound
    oDog.Sound

End Sub

Output:
meowww
bark bark
 
Last edited:
Upvote 1
Solution
In addition to @Jaafar Tribak 's excellent post, I might add that you could also declare a parameter as an optional Variant. This would allow you to use the IsMissing fuction to check whether the argument has been passed to the procedure, in addition to checking for the data type using TypeName. Then, based on the data being passed to the procedure, you could execute the appropriate code or call another procedure, passing it the relevant variables.

VBA Code:
Sub test()

    Dim a As Integer
    Dim b As Double
    Dim c As String
   
    a = 200
    b = 12.5
    c = "red"
   
    foo a, b, c
   
    foo a, b
   
    foo a
   
    foo a, , c
   
End Sub

Sub foo(v1 As Variant, Optional v2 As Variant, Optional v3 As Variant)

    Debug.Print TypeName(v1), IsMissing(v2), IsMissing(v3)
   
End Sub

Here's the output...

Code:
Integer       False         False
Integer       False         True
Integer       True          True
Integer       True          False

Hope this helps!
 
Upvote 0
Hello,

Thank you both for your answers!

Jaafar, very precise message with clear examples, it answers my interrogations. Also i was not aware of the ParamArray for which i already have many uses, as i was simply defining my arrays parameters as variants. For the moment i hadn't touch the Implementations as i had no use for it and it looked a bit clumsy, but you gave me a good example on how it is done in VBA.

Domenic thank you too, i was actually already aware of this "trick" with the isMissing keyword only working with variant types, but very practical. I used it for an RGB picker/randomizer.

I consider the topic solved, thanks again!
 
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top