1

Apologies if I am making some amateur mistakes, but I am new to VBA. I'm trying to populate an array declared inside of a class as a property, but something's going wrong. After much searching I have two questions that I can't seem to find anywhere else:

1 - Is there a simpler way to accomplish saving data to an array-like structure that I can pass to functions from a sub? The array-like structure needs to be resizable because I will not know how many components I will add until each iteration of a loop checks those conditions.
2 - How do I correctly accomplish the passing of the array property in my class correctly to another function? It's frustrated me enough that I would like to know how it can be done if only to understand what I was doing wrong or, perhaps, what misunderstanding I had of the way VBA works.

Code structure follows:

I have declared a Segments property inside of a CTask class like this:

Private pSegments() As CSegment

Public Property Get Segments() As CSegment()
    Segments = pSegments()
End Property
Public Property Get segment(index As Integer) As CSegment
    segment = pSegments(index)
End Property
Public Property Let Segments(Value() As CSegment)
    pSegments() = Value()
End Property

I pass the CTask from a Sub, where it is defined to populateTasks using this code:

Dim tasks() As CTask
ReDim tasks(1 To 10)
Call populateTasks(tasks)

When I do this, the populateTasks code receives it using the following code:

Function populateTasks(ByRef tasks() As CTask)

I then try to call another function from populateTasks called populateSegments like this:

Call populateSegments(tasks(icount).Segments)

I receive segments array inside populateSegments like this:

Function populateSegments(ByRef Segments() As CSegment)

The last two code snippets are where the problem resides. The segments array is populating correctly inside the populateSegments function, but when I check the array to see if it is empty just below the call to populateSegments there isn't anything in the tasks(icount).segments array. Thanks in advance for any help, and please let me know if more information is required.

Aterxerxes
  • 554
  • 4
  • 13
  • Your property returns a copy of the array. You need to populate the copy and then assign that back to the property in order to update the array in the class. – Rory Sep 11 '14 at 14:53
  • @Rory So I need to use AndyBrazil's solution or something similar? – Aterxerxes Sep 11 '14 at 15:06
  • You don't have to (you can do what I described) but it makes sense to have it as a method of the cTask class. – Rory Sep 11 '14 at 15:59
  • @Rory So how would assign it back to the property in the correct way? I tried and kept getting errors. – Aterxerxes Sep 11 '14 at 16:40

1 Answers1

1

Couldn't you make populatesegments a method of cTask? That avoids the problem and is a better design

Andy Brazil
  • 134
  • 2
  • Could you give me a small example of the code to correctly pass the array to that method and the method signature so that I can be sure to do it correctly? I've tried numerous times, but keep getting errors. – Aterxerxes Sep 12 '14 at 15:32
  • This was indeed helpful in my answer, but I would also like to point to another that also helped me zero in on the problem [here](http://stackoverflow.com/questions/5613564/vba-returning-array-from-property-get). After I made the populateSegments a method of cTask, I wasn't getting an item from pSegments, given the index, for some reason. I created a custom method, rather than a property, to simply return one item from pSegments, given an index, and it works just fine now. Thanks for your help, everyone. – Aterxerxes Oct 22 '14 at 17:42