Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function returning an array

Status
Not open for further replies.

DKL01

Programmer
Sep 14, 2000
233
US
Hi,

I'm wondering what's wrong with this code:

Public Function process()

Dim arr1() As String
ReDim arr1(2)

arr1(0) = "header1"
arr1(1) = "header2"
arr1(2) = "header3"

process = arr1

End Function

Private Sub Form_Load()

Dim arr2() As String
arr2 = process()

End Sub

Thanks
Dilip
 
Look at the following procedures. Especially, READ the comments

This is just a replacement for you form load procedure. Renamed ONLY to facillate demonstration of the functions, although you COULD call something LIKE this from your [Form_Load] procedure.

Code:
Public Function ProcessTest()

    'Renamed from "Form_Load" to facillate Testing/demonstration

    Dim arr2() As String
    'Here, again, you attempt to assign a value to an array (arr2()).
    'In this instance, the array is not even dimensioned.
    'arr2 = Process()

    'This version "works", in that "MyArrVar" is given the value "header1"
    Dim MyArrVar As String
    MyArrVar = Process()
    Debug.Print MyArrVar        'Returns "header1"


    'I'm completly unsure what you are attempting to accomplish.
    'As a GUESS, you may be attempting to retrieve a selected
    'string (header [1 | 2 | 3], from the set stored as literals
    'in [Process].  To do this, I would modify [Process] to accept
    'an integer arg to use as the index into the arr1().

    'This (and other related functions) would then call [Process(n)]
    'with "n" set to the index of the string they want/need to retrieve.
    'So, assuming 'This process' needed to retrieve arr1(2):

    MyArrVar = ProcessWIndex(2)
    Debug.Print MyArrVar        ''Returns "HeaderC"

End Function

'A discussion of what - specifically - doesn't work in the code posted.
Code:
Public Function Process()

    'I 'm wondering what's wrong with this code:


    Dim Arr1() As String
    ReDim Arr1(2)

    Arr1(0) = "header1"
    Arr1(1) = "header2"
    Arr1(2) = "header3"

    'Process = arr1         'Doesn't work, because arr1 is an arrayed variable
                            'and thus requires an Index
    Process = Arr1(0)       'This "works", in that it returns the String "header1"

End Function


'What I THINK you intend, but it is JUST a wild guess?
Code:
Public Function ProcessWIndex(Indx As Integer) As String

    Dim Arr1() As String
    ReDim Arr1(2)

    Arr1(0) = "HeaderA"
    Arr1(1) = "HeaderB"
    Arr1(2) = "HeaderC"

    ProcessWIndex = Arr1(Indx)       'This "works", as I THINK you are tring to do it?

End Function

MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
in the Form_Load :

Dim arr2() As String
Dim iCounter As Integer
Dim strResult As String
arr2 = process()
For iCounter = LBound(arr2) To UBound(arr2)
strResult = arr2(iCounter)
Next

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Eric,

I'm getting compilation error "Can't assign to array" at
arr2 = process()

 
I not and i declareer "Option Explicit on Generall "

by me its works ( i have a better function to do that)Later...
Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Meanwhile let me explain you guys what I'm tring to do.

01. I need to write a function ( in VB activex dll ) which retuns an array .

02. Call this function from an ASP and assign the array retuned by this dll to an array defined in asp.

Thanks
Dilip
 
To have a VB function return an ARRAY, you need to define a variable of variant type:

Dim MyVal as Variant[tab][tab][tab]'Note this is NOT declared as an array.


You then assing the variable to the function which will return the arrray:

MyVal = Process[tab][tab]'Note: No Args-but this may vary depending on the function implementation

You then need to DECLARE the return value of the function as Variant:

Public Function Process(arg1, arg2, ... argN) As Variant

When the function returns the array, you will be able to access the elements just as you would if the original declaration included the array indication:

For Idx = 0 to UBound(MyVal)
[tab]Debug.Print MyVav(Idx)[tab][tab]'or any REAL processing
Next Idx




MichaelRed
mred@duvallgroup.com
There is never time to do it right but there is always time to do it over
 
MichaelRed, Eric

I got it. Thank you very much.

Dilip
 
Dilip

a Tip for the Function :

Dim strHeaders As String
Dim arr1() As String
strHeaders = "header1,header2,header3"
arr1 = Split(strHeaders, ",")
process = arr1

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top