4

I tried to overload in VBA within a class module:

Dim a As Integer

Public Property Let SomeNumber(newNumber as Integer)
   a = newNumber
End Property
Public Property Let SomeNumber(newNumber as String)
   a = Val(newNumber)
End Property
Public Property Get SomeNumber() As Integer
   SomeNumber = a
End Property

The compiler complains that an "ambiguous name was detected" where clearly there is a different signature. Is it possible to overload a property defined within a class in VBA or VB6? If so, what would be the syntax?

Moreover, if property overloading is not possible, what benefits do properties offer over get/set methods defined by public functions other than a more seamless way to access the fields of an instantiated object?

Scott Conover
  • 1,421
  • 1
  • 14
  • 27
Chad Harrison
  • 2,836
  • 4
  • 33
  • 50

4 Answers4

2

Can you use a variant or object type and manually handle the type checking?

A mixture of Optional parameters may also be possible, but it's not exactly equivalent to overloading either.

Enull
  • 256
  • 1
  • 5
2

No.

The workaround is to use Variants, which allows the client to pass anything. But you have to write code to do the type-checking at runtime:

Public Property Let SomeNumber(newNumber as Variant)
   Select Case VarType(newNumber) 
      Case vbInteger         '' Caller passed an integer
        a = newNumber
      Case vbString          '' Caller passed a string
        a = Val(newNumber)
      Case Else
        Err.Raise vbObjectError+513, , "Invalid type passed"
      End Select
End Property
Public Property Get SomeNumber() As Variant
   SomeNumber = a
End Property

For more details see Dan Appleman's excellent book Developing COM/ActiveX Components In Visual Basic 6.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
1

From what I can see here:

http://vbcity.com/forums/t/76453.aspx

It seems you are out of luck until you convert to VB.NET.

AZhu
  • 1,312
  • 6
  • 22
  • 40
1

There was a thread on this topic:

Function Overloading and UDF in Excel VBA

Community
  • 1
  • 1
MPękalski
  • 6,873
  • 4
  • 26
  • 36