Property Overload

I tried overloading in VBA in 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 found where, obviously, another signature exists. Is it possible to overload a property defined in a class in VBA or VB6? If so, what will be the syntax?

In addition, if property overloading is not possible, what advantages do properties offer with get / set methods defined by public functions other than a simpler way to access the fields of the created object?

+3
source share
4 answers

From what I see here:

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

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

+1
source

?

, .

+2

There was a topic in this section:

Function overloading and UDF in Excel VBA

+1
source

No.

The workaround is to use options that allow the client to convey something. But you need to write code for 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 information, see Dan Eppmann Designing COM / ActiveX Components in Visual Basic 6 .

+1
source

All Articles