Solved

How to create an array function

Posted on 2011-09-30
8
184 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
8 Comments
 
LVL 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 200 total points
Comment Utility
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
Comment Utility
A few different ways but this is one:

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

YOUR app

yourFunction = vArray


End function
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
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
 
LVL 1

Author Comment

by:Milewskp
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

Expert Comment

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

Author Comment

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

Accepted Solution

by:
aikimark earned 300 total points
Comment Utility
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
Comment Utility
Thanks everyone!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now