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()?
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!


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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

832 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