Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to create an array function

Posted on 2011-09-30
8
Medium Priority
?
223 Views
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.
0
Comment
Question by:Milewskp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 800 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.
0
 
LVL 10

Expert Comment

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

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

YOUR app

yourFunction = vArray


End function
0
 
LVL 46

Expert Comment

by:aikimark
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()?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Milewskp
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?
0
 
LVL 46

Expert Comment

by:aikimark
ID: 36902976
click the help about menu.  The version will be displayed in the subsequent dialog box.
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
aikimark earned 1200 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
    Next
    ArrayTest = lngReturn
End Function

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

Open in new window

0
 
LVL 1

Author Closing Comment

by:Milewskp
ID: 36903691
Thanks everyone!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

618 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