Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your approach stores a collection of ClassB instances in an array. For each instance you add, you must first ReDim the array. ReDim is an expensive operation, and will become even more expensive as the number of array members grows. That wouldn't be much of an issue if the array only ever held a single ClassB instance. OTOH, if you don't intend more than one ClassB instance, what is the point of storing it in an array? </p> <p>It makes more sense to me to store the collection of instances in a VBA Collection. Collections are fast for this, and aren't subject to the dramatic slow downs you will encounter with an array as the number of items grows.</p> <p>Here is a Collection approach for clsClassA. </p> <pre><code>Option Compare Database Option Explicit Private mcolA As Collection Private Sub Class_Initialize() Set mcolA = New Collection End Sub Private Sub Class_Terminate() Set mcolA = Nothing End Sub Public Function NewB(ByVal i As Integer) As Object Dim objB As clsClassB If i &gt; mcolA.Count Then Set objB = New clsClassB mcolA.Add objB Else Set objB = Nothing End If Set NewB = objB Set objB = Nothing End Function </code></pre> <p>The only change I made to clsClassB was to add <strong>Option Explicit</strong>. </p> <p>This procedure uses the class.</p> <pre><code>Public Sub test_ClassA_NewB() Dim a As clsClassA Dim b As clsClassB Set a = New clsClassA Set b = a.NewB(1) '' Collections are one-based instead of zero-based Debug.Print TypeName(b) ' prints clsClassB Debug.Print b.data '' prints 0 b.data = 27 Debug.Print b.data '' prints 27 Set b = Nothing Set a = Nothing End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload