title: Array as a Class Member
body: <p>I'm designing a dynamic buffer for outgoing messages.  The data structure takes the form of a queue of nodes that have a Byte Array buffer as a member.  Unfortunately in VBA, Arrays cannot be public members of a class.</p>
  
  <p>For example, this is a no-no and will not compile:</p>
  
  <pre><code>'clsTest
  
  Public Buffer() As Byte
  </code></pre>
  
  <p>You will get the following error: "Constants, fixed-length strings, arrays, user-defined types and Declare statements not allowed as Public members of object modules"</p>
  
  <p>Well, that's fine, I'll just make it a private member with public Property accessors...</p>
  
  <pre><code>'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
  </code></pre>
  
  <p>...and then a few tests in a module to make sure it works:</p>
  
  <pre><code>'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's 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
  </code></pre>
  
  <p>Test #1 works fine, <del>but Test #2 breaks, <code>Buffer</code> is highlighted, and the error message is "Wrong number of arguments or invalid property assignment"</del></p>
  
  <p>Test #2 now works! GSerg points out that in order to call the <code>Property Get Buffer()</code> correctly and also refer to a specific index in the buffer,  <strong>TWO</strong> sets of parenthesis are necessary: <code>oBuffer.Buffer()(2)</code></p>
  
  <p>Test #3 fails - the original value of 3 is printed to the Immediate window. GSerg pointed out in his comment that the <code>Public Property Get Buffer()</code> only returns a copy and not the actual class member array, so modifications are lost.</p>
  
  <p><strong>How can this third issue be resolved make the class member array work as expected?</strong></p>
  
  <p><em>(I should clarify that the general question is "VBA doesn't allow arrays to be public members of classes.  How can I get around this to have an array member of a class that behaves as if it was for all practical purposes including: #1 assigning the array, #2 getting values from the array, #3 assigning values in the array and #4 using the array directly in a call to <code>CopyMemory</code> (#3 and #4 are nearly equivalent)?)"</em></p>