1

I try to write an array property and running my code I always get the error that I cannot assign to a write-protected Property.

My code:

Private pQ(9) As String

Public Property Get Q() As String()
    Q() = pQ()
End Property
Public Property Let Q(value() As String())
    pQ() = value()
End Property

I thought it is because of the parentheses so I tried all combinations but it didnt worked. Anyone who can help me with this, please?

This is where I get the error:

For j = 0 To 9
.Q(j) = ThisWorkbook.Worksheets("xx").Cells(3, j + 1)
Next j
1
  • The odd thing is that Property Let Q(value() As String()) doesn't seem to be correct syntactically. Second odd thing is you try to assign the reference of a dynamic array to a statically allocated array. Commented Jun 23, 2014 at 14:04

1 Answer 1

3

You cannot use arrays in property pairs.

If you could, your example would pass around new copies of the backing array which would not be very efficient.

You only need to pass around the index and value then use these to read and write to the internal array:

Public Property Get Q(index As integer) As String
    Q = pQ(index)
End Property

Public Property Let Q(index As integer, value As String)
    pQ(index) = value
End Property
Sign up to request clarification or add additional context in comments.

1 Comment

@Alex K. Your solution works and what I understand is that property get cannot return an array. Is my understanding correct? Also what is property pairs?

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.