2

I have a class where one of the member variables is an array. I am trying to assign an array to the object but keep getting the 'Can't assign array' compile error. Also I was curious as how to get UBound of the array in object. UBound(obj.array) doesn't compile. I am using excel 07 vba.

'Test routine that keeps failing

Sub test()  

Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"

Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4    
MsgBox UBound(obj.ary)

End Sub  


'Class declaration
Private pary() As String
Private pfield As Long

Public Property Get ary(ByVal index As Long) As String
    Set ary = pary(index)
End Property

Public Property Let avry(Value() As String)
    ReDim pary(UBound(Value)) As String
    For i = LBound(Value) To UBound(Value)
        pary(i) = Value(i)
    Next i
End Property

Public Property Get field() As Long
    field = pfield
End Property

Public Property Let field(Value As Long)
    pfield = Value
End Property

Private Sub Class_Initialize()
    pfield = 0
End Sub

2 Answers 2

1

This worked for me

Sub test()

Dim Arr(2) As String
Arr(0) = ""
Arr(1) = "Pizza"
Arr(2) = "Hoes"

Dim obj As Cats
Set obj = New Cats
obj.avry = Arr
obj.field = 4
MsgBox obj.ary(2)

End Sub

Public Property Get ary(ByVal index As Long) As String
    ary = pary(index)
End Property

Public Property Let avry(vValue As Variant)
    ReDim pary(UBound(vValue)) As String
    Dim i As Long
    For i = LBound(vValue) To UBound(vValue)
        pary(i) = vValue(i)
    Next i
End Property

Public Property Get field() As Long
    field = pfield
End Property

Public Property Let field(Value As Long)
    pfield = Value
End Property

Private Sub Class_Initialize()
    pfield = 0
End Sub

As Tim said, you can pass the array as a variant. Your MsgBox is trying to find a UBound of a String data type, so that was a problem. Also, you weren't passing an argument to ary in the MsgBox. The ary property returns a String, but you were using the Set keyword, which was another problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this worked. Instead of using UBound, I added a third member variable that acts as a counter in the for loop of the assignment operator for the array. Also I had to declare pary as pary()
0

There seems to be an issue with passing array parameters to class properties.

You can get around this by switching the Let parameter to a Variant:

Public Property Let avry(ByRef arrVal As Variant)
    Dim i As Integer
    If IsArray(arrVal) Then
        ReDim pary(LBound(arrVal) To UBound(arrVal))
        For i = LBound(arrVal) To UBound(arrVal)
            pary(i) = arrVal(i)
        Next i
    End If
End Property

2 Comments

tried your solution and am still getting the same error. I should mention i was getting a 'Definitions of property procedures for the same property are inconsistent' error before when I was using ary for both the get and let properties. Changed the get to avry as a workaround. Public Property Get ary(ByVal index As Long) As String Public Property Let ary(ByRef Value() As Variant) gives that error. Maybe this has a connection with array assignment problem.
You should use different property names if the Let/Set and Get types are different (array vs. string in your case). That's what your original 'inconsistent definitions' error was telling you. Note there's a problem with your Msgbox() call in test(): your property ary Get returns a string, but you're trying to treat it as an array by calling UBound() on the return value.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.