I am developing a dynamic buffer for outgoing messages. The data structure takes the form of a node queue in which a byte array buffer is used as a member. Unfortunately, in VBA arrays cannot be public members of a class.
For example, it is no-no and will not compile:
'clsTest Public Buffer() As Byte
You will receive the following error: "Constants, fixed-length strings, arrays, user-defined types and Declare statements that are not allowed as" Public members of object modules "
Ok, thatβs fine, Iβll just make it a private member with the Property Property Accessory ...
'clsTest Private m_Buffer() As Byte Public Property Let Buffer(buf() As Byte) m_Buffer = buf End Property Public Property Get Buffer() As Byte() Buffer = m_Buffer End Property
... and then a few tests in the module to make sure it works:
'mdlMain Public Sub Main() Dim buf() As Byte ReDim buf(0 To 4) buf(0) = 1 buf(1) = 2 buf(2) = 3 buf(3) = 4 Dim oBuffer As clsTest Set oBuffer = New clsTest 'Test #1, the assignment oBuffer.Buffer = buf 'Success! 'Test #2, get the value of an index in the array ' Debug.Print oBuffer.Buffer(2) 'Fail Debug.Print oBuffer.Buffer()(2) 'Success! This is from GSerg comment 'Test #3, change the value of an index in the array and verify that it is actually modified oBuffer.Buffer()(2) = 27 Debug.Print oBuffer.Buffer()(2) 'Fail, diplays "3" in the immediate window End Sub
Test # 1 works fine, , but tags # 2, Buffer highlighted, and the error message is "Invalid number of arguments or invalid property assignment"
Test # 2 now works! GSerg indicates that in order to correctly call Property Get Buffer() , as well as to specify a specific index in the buffer, TWO strong> brackets are needed : oBuffer.Buffer()(2)
Test No. 3 does not work - the initial value of 3 is printed in the Immediate window. In a comment, GSerg pointed out that Public Property Get Buffer() returns a copy, not an actual array of class members, so changes are lost.
How to solve this third problem so that the array of class members works as expected?
(I must clarify that the general question is: βVBA does not allow arrays to be public members of classes. How can I get around this to have a member of the class array that behaves as if it were for all practical purposes including: 1 assignment of the array, # 2 receiving values ββfrom the array, # 3 assigning values ββin the array and # 4 using the array directly in the CopyMemory call (# 3 and # 4 are almost equivalent)? ""