How to create an array function

Posted on 2011-09-30
Last Modified: 2012-05-12
Is it possible to create a custom function that returns an array, so that I can assign that function to an array variable?

Note to self: If it is possible, revise XRisk() accordingly.
Question by:Milewskp
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 200 total points
ID: 36894804
Yes, you can.  I typically do it by:

1) Declaring the function as Variant
2) Declaring an array variable within the function to hold the "answer"
3) Setting the return value of the function to that array variable

Then, if I call that function from elsewhere in my VB project, I make sure to declare the variable accepting the result as a Variant.
LVL 10

Expert Comment

ID: 36896099
A few different ways but this is one:

Function yourFunction () as variant ()
Dim vArray as variant

YOUR app

yourFunction = vArray

End function
LVL 45

Expert Comment

ID: 36901160
What version of VB is this.  In VB5 (and earlier versions) you had to return a Variant data type as noted by matthewspatrick.  In VB6, you were allowed to return an array.

What is XRisk()?
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.


Author Comment

ID: 36902939
Hi, aikimark
XRisk() is just the name of a function I'm using. How can I tell if I'm using VB5 or 6? Can you show me an example of a VB6 array function?
LVL 45

Expert Comment

ID: 36902976
click the help about menu.  The version will be displayed in the subsequent dialog box.

Author Comment

ID: 36902990
I have VB 6.5, Version 1053? Can you show me an example of a VB6 array function?
LVL 45

Accepted Solution

aikimark earned 300 total points
ID: 36903132
Public Function ArrayTest(ByVal parmSize As Long) As Long()
    Dim lngLoop As Long
    Dim lngReturn() As Long
    ReDim lngReturn(1 To parmSize)
    For lngLoop = 1 To parmSize
        lngReturn(lngLoop) = lngLoop
    ArrayTest = lngReturn
End Function

Public Sub testarray()
    Debug.Print UBound(ArrayTest(100))
    Debug.Print ArrayTest(105)(3)
End Sub

Open in new window


Author Closing Comment

ID: 36903691
Thanks everyone!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question