2

I'm having a hard time setting and calling a String array class property properly.

Current Class Property

Private pNames() As String

'pNames Properties
Public Property Get Names() As String
    Names() = pNames
End Property
Public Property Let Names(Names As String)
    pNames() = Names
End Property

Current Compilation Error

Compile Error: Expected array

on the below line, specifically highlighting LBound:

For Loop1 = LBound(EditCategories.Names) To UBound(EditCategories.Names)

How can I register this property properly as a String array?


What I've Tried

Public Property Let Names()(Names As String) which highlights the empty parentheses and throws:

Argument required for Property Let or Property Set

Public Property Let Names(Names() As String) which highlights the entire line and throws:

Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalid Set final parameter

1 Answer 1

4

Backing field is a String array. VBA syntax for typed arrays is a bit weird/inconsistent.

Backing field:

Private pNames() As String

Property:

Public Property Get Names() As String()

Notice the String() parens - that would be illegal in the Private pNames declaration - go figure.

The problem is that you can't assign to an array like this, so there's no way a Property Let accessor can be legal - this wouldn't compile:

Public Property Let Names(ByRef values As String())

Welcome to the joys of typed arrays in VBA. Just expose it as a Variant and be done with it:

Public Property Get Names() As Variant
Public Property Let Names(ByRef values As Variant)
Sign up to request clarification or add additional context in comments.

3 Comments

FWIW - although Public Property Let Names(ByRef values As String()) is invalid, switching it to Public Property Let Names(ByRef values() As String) will compile, but then you get stuck when you try to use it, e.g. x.Names = y (where y is a String array) gives the "Can't assign to an array" compile error. VBA can be frustrating at times!
For most values of "times", @YowE3K!
@FreeMan For all of the values of "most"!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.