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
Solved

Function resturn array...

Posted on 2000-03-31
5
216 Views
Last Modified: 2010-05-02
How can I set up a function that returns an array?
0
Comment
Question by:mhdhallak
5 Comments
 
LVL 32

Accepted Solution

by:
Erick37 earned 8 total points
ID: 2675244
By passing the array ByRef, you can have a function modify the array passed to it as a parameter:

Option Explicit

Private Sub Command1_Click()
    Dim a(10) As Long
    Call ModifyArray(a)
    Debug.Print a(5)
End Sub

Private Function ModifyArray(a() As Long)
    Dim i As Long
    For i = LBound(a) To UBound(a)
        a(i) = a(i) + 1
    Next
End Function
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2675260
In VB6 arrays can be passed by reference. When you do a ByRef, the subroutine then updates the actual array in the calling procedure. Below is an example.. just copy and paste it into a New Standard.Exe project and run it.. <smile>

<----- Code Begin ----->

Option Explicit

Private Sub Form_Load()

Dim strWork() As String
Dim intWork() As Integer
ReDim intWork(3)
intWork(0) = 1
intWork(1) = 2
intWork(2) = 3
Call MySubroutine(intWork(), strWork())

MsgBox (intWork(0) & " = " & strWork(0) & vbCrLf _
   & intWork(1) & " = " & strWork(1) & vbCrLf _
   & intWork(2) & " = " & strWork(2) & vbCrLf)

End Sub

Private Sub MySubroutine _
(ByRef intNumbers() As Integer, _
ByRef strStrings() As String)

   Dim intIndex As Integer
   ReDim strStrings(UBound(intNumbers))
   For intIndex = 0 To UBound(intNumbers)
     strStrings(intIndex) = intNumbers(intIndex) & " *** "
     intNumbers(intIndex) = intNumbers(intIndex) + 10
   Next intIndex
   
End Sub

<----- Code End ----->
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2675267
You can also do this by using Variants.. an Example follows:

<----- Code Begin ----->

Option Explicit

Private Sub Form_Load()

Dim strWork() As String
Dim intWork() As Integer
Dim varWork As Variant
ReDim intWork(3)
intWork(0) = 1
intWork(1) = 2
intWork(2) = 3
varWork = intWork()
strWork() = MyFunction(varWork)

MsgBox (intWork(0) & " = " & strWork(0) & vbCrLf _
   & intWork(1) & " = " & strWork(1) & vbCrLf _
   & intWork(2) & " = " & strWork(2) & vbCrLf)

End Sub

Private Function MyFunction _
(ByRef varTable As Variant) _
As Variant

   Dim intNumbers() As Integer
   intNumbers() = varTable
   
   Dim strStrings() As String
   ReDim strStrings(UBound(intNumbers))
   
   Dim intIndex As Integer
   For intIndex = 0 To UBound(intNumbers)
     strStrings(intIndex) = intNumbers(intIndex) & " *** "
     intNumbers(intIndex) = intNumbers(intIndex) + 10
   Next intIndex
   
   MyFunction = strStrings()

End Function

<----- Code End ----->
0
 

Author Comment

by:mhdhallak
ID: 2676001
Sorry guys. But Eric was the first one to answer the question and I worked out his answer and it was right.

0
 
LVL 10

Expert Comment

by:caraf_g
ID: 5150881
Erick37 - would you mind sending me an e-mail please, it's quite urgent. deathtospammers@eircom.net
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 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