Title: VBA - passing objects between functions
Tags: arrays;vba;class;object;ms-access-2007
Question: First question here.  I've been looking for answers to this question for days, and have worked out MOST of what I've been trying to do myself.  I'd like to share what I found, and hope that someone can suggest a better way to make this work.

I have a function that uses several of one class, and then needs to return this information in an array for use elsewhere.  The code that uses this needs to break this information back down, and reuse the information returned.  The reason it's not all one block of code is that the function is called many times without asking for the return values.

The part of the Class module that we are concerned about is below.  This is the ONLY way I have been able to get it to work.  Mind the parentheses and count the parameters.

```Option Compare Database
Option Explicit

Private p_intaryAgentNames() As Integer

Public Property Get AgentNames() As Variant
        AgentNames = p_intaryAgentNames()
End Property

Public Property Let AgentNames(IncomingArray As Variant)
  If IsArray(IncomingArray) Then
        If UBound(p_intaryAgentNames) <&gt; UBound(IncomingArray) Then ReDim Preserve p_intaryAgentNames(UBound(IncomingArray))
        p_intaryAgentNames() = IncomingArray
    Else
        MsgBox ("Invalid information passed to AgentNames array")
    End If
End Property

Public Property Get Filter() As String
    Filter = p_strFilter
End Property

Public Property Let Filter(value As String)
    p_strFilter = value
End Property

Private Sub Class_Initialize()

    ReDim p_intaryAgentNames(0)

End Sub
```

When finishing my function, this is how I use my object members, put them into an array, then pass it to the calling code:

```Function CalculateRecords() As Variant

Const CONSTANTSTRINGZEROLENGTH As String = ""

Dim objRec1 As cAgent
Dim objRec2 As cAgent
Dim objRec3 As cAgent

Set objRec1 = New cAgent
Set objRec2 = New cAgent
Set objRec3 = New cAgent

Dim objAgents(2) As cAgent
'some things happen
Set objAgents(0) = objRec1
Set objAgents(1) = objRec2
Set objAgents(2) = objRec3


CalculateRecords = objAgents
End Function
```

This is how I plan to unpack the data, but it doesn't seem like I'm using it right:

Private Sub cmdAssignRecords_Click()

```Dim cAgentInfo As Variant
Dim objRec1 As cAgent
Set objRec1 = New cAgent
Dim objRec2 As cAgent
Set objRec2 = New cAgent
Dim objRec3 As cAgent
Set objRec3 = New cAgent

cAgentInfo = CalculateRecords() 'A variant to catch a variant

Set objRec1 = cAgentInfo(0) 
Set objRec2 = cAgentInfo(0) 
Set objRec3 = cAgentInfo(0) 
```

What I would rather do is use cAgentInfo() directly, and this works for other objects of the class.  What I cannot do is access cAgentInfo(0).AgentNames(0), while objRec1.AgentNames(0) and cAgentInfo(0).filter work just fine.  

I'm sure I'm just missing something here, or maybe I'm just throwing myself against a wall.  Any suggestions as to what I'm missing, or how I can improve are well-appreciated.  I feel like doing it this way will improve readability, but isn't it also wasting namespace?


Here is the accepted answer: I can't catch your big design, so I try suggesting

```Private Sub cmdAssignRecords_Click()
'Dim cAgentInfo As Variant
Dim cAgentInfo() As cAgent
...
```

and

```'Function CalculateRecords() As Variant
Function CalculateRecords() As cAgent()
```

that way you can use cAgentInfo(0).AgentNames(0) in function ```CalculateRecords```

Comment for this answer: This is what worked.  I was not aware I could declare the function as an array of the class I created.  I thought I could only pass arrays as a variant.

Here is another answer: Try declaring

```Dim cAgentInfo As Variant 
```

as an array like this:

```Dim cAgentInfo() As Variant
```

Comment for this answer: I tried that, as it seemed to be the most straightforward approach.  Unfortunately, I could not assign the variant array returned by the function to a new variant array.  Paraphrasing, I got an error stating that I could not assign to an array.  It works though when I change cAgentInfo to a cAgent data type.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:03/16/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<arrays><vba><class><object><ms-access-2007>', 'Title': 'VBA - passing objects between functions', 'ViewCount': 149, 'id': 36046329, 'user_AboutMe': None, 'user_AccountId': 8056555.0, 'user_DisplayName': 'sweBers', 'user_Reputation': 13.0, 'user_Views': 2.0, 'user_id': 6073502.0}}