pass an array back from function 
Author Message
 pass an array back from function

Hi,

I'm pretty new at this and have been doing some web work using ASP and
Visual Basic. Would like to be able to call a function and have it
return an array back to the caller. I've seen examples of this kind of
thing but haven't been able to make it work. BTW, the server I'm using
has VB 5 installed.

Thanks in advance,

Marty



Mon, 26 Jul 2004 09:10:20 GMT  
 pass an array back from function

Quote:

> Would like to be able to call a function and have it return an array back
> to the caller.

I can think of two ways to do this.

1. Use the Variant data type. You can store an array in a Variant, and you
can return a Variant from a function. e.g.

Dim S As Variant
S = GetMyStrings()
Debug.Print S(0) & ", " & S(1) & ", " & S(2)

Function GetMyStrings() As Variant
Dim TestStrings(3) As String
TestStrings(0) = "first string"
TestStrings(1) = "second string"
TestStrings(2) = "third string"
GetMyStrings = TestStrings
End Function

2. Create an empty array in the calling code and pass it to the function by
value (ByVal keyword) instead of by reference. You can then modify that same
array (using ReDim and so forth) within the function. There's no need to use
a return value in this case.

Eq.

--

Equinox Tetrachloride
Not sociophobic, just avoiding you.
www.cl4.org - www.insecurities.org



Mon, 26 Jul 2004 09:33:29 GMT  
 pass an array back from function


Quote:
> Hi,

> I'm pretty new at this and have been doing some web work using ASP and
> Visual Basic. Would like to be able to call a function and have it
> return an array back to the caller. I've seen examples of this kind of
> thing but haven't been able to make it work. BTW, the server I'm using
> has VB 5 installed.

> Thanks in advance,

With VB5 you can do something like this, if you want it as a return value:

Function ReturnTheArray() As Variant
    ReturnTheArray = Array(1,2,3,4)
End Function

HTH,
Tom Shelton



Mon, 26 Jul 2004 11:25:15 GMT  
 pass an array back from function
I'm not getting this to work so far.

Have tried the following two programs :

=============================================

<%
Option Explicit

Dim S As Variant
S = GetMyStrings()
Debug.Print S(0) & ", " & S(1) & ", " & S(2)

Function GetMyStrings() As Variant
        Dim TestStrings(3) As String
        TestStrings(0) = "first string"
        TestStrings(1) = "second string"
        TestStrings(2) = "third string"
        GetMyStrings = TestStrings
End Function

%>

at http://feedthebulldog.com/asp/passArray2.asp

gets the response :

Microsoft VBScript compilation error '800a0401'

Expected end of statement

/asp/passArray2.asp, line 4

Dim S As Variant
------^

=============================================

<%
Option Explicit

Dim Nums(4),num
Nums = ReturnArray()
For Each num in Nums
        response.write "<br>Number eq " & num
Next

Function ReturnArray() As Variant
    ReturnArray = Array(1,2,3,4)
End Function

%>

at http://feedthebulldog.com/asp/passArray.asp

gets this response

Microsoft VBScript compilation error '800a0400'

Expected statement

/asp/passArray.asp, line 10

Function ReturnArray() As Variant
-----------------------^

So what am I doing wrong? Sorry I didn't try out the other
suggestions, but at this point feeling much more comfortable with copy
& paste type solutions.

Thanks,

Marty



Mon, 26 Jul 2004 18:17:11 GMT  
 pass an array back from function

Quote:

> I'm not getting this to work so far.
> [...]
> Microsoft VBScript compilation error '800a0400'

It looks like VBScript doesn't support VB's Variant type. Unfortunately, I
can't help with VBScript as I've never used it, so I'm not sure of the
differences.

Eq.

--

Equinox Tetrachloride
Not sociophobic, just avoiding you.
www.cl4.org - www.insecurities.org



Mon, 26 Jul 2004 23:35:58 GMT  
 pass an array back from function
Without knowing anything about asp or VB script, I'll simply mention that
VB6 supports returning a string array from a function

Function MyStrings() as String()


Quote:

> > I'm not getting this to work so far.
> > [...]
> > Microsoft VBScript compilation error '800a0400'

> It looks like VBScript doesn't support VB's Variant type. Unfortunately, I
> can't help with VBScript as I've never used it, so I'm not sure of the
> differences.

> Eq.

> --

> Equinox Tetrachloride
> Not sociophobic, just avoiding you.
> www.cl4.org - www.insecurities.org



Mon, 26 Jul 2004 23:42:55 GMT  
 pass an array back from function
Marty

You don't declare the return type of functions in VBScript, they always
return a variant, plus ALL variables are Variants, so your function should
be

Function GetMyStrings()
Dim TestStrings(3)
TestStrings(0) = "first string"
TestStrings(1) = "second string"
TestStrings(2) = "third string"
GetMyStrings = TestStrings
End Function

regards

Ian Williams

** invalid reply address, change dk to denmark

Tips & Tricks - http://tips.kingsoft-denmark.com


Quote:
> I'm not getting this to work so far.

> Have tried the following two programs :

> =============================================

> <%
> Option Explicit

> Dim S As Variant
> S = GetMyStrings()
> Debug.Print S(0) & ", " & S(1) & ", " & S(2)

> Function GetMyStrings() As Variant
> Dim TestStrings(3) As String
> TestStrings(0) = "first string"
> TestStrings(1) = "second string"
> TestStrings(2) = "third string"
> GetMyStrings = TestStrings
> End Function

> %>

> at http://feedthebulldog.com/asp/passArray2.asp

> gets the response :

> Microsoft VBScript compilation error '800a0401'

> Expected end of statement

> /asp/passArray2.asp, line 4

> Dim S As Variant
> ------^

> =============================================

> <%
> Option Explicit

> Dim Nums(4),num
> Nums = ReturnArray()
> For Each num in Nums
> response.write "<br>Number eq " & num
> Next

> Function ReturnArray() As Variant
>     ReturnArray = Array(1,2,3,4)
> End Function

> %>

> at http://feedthebulldog.com/asp/passArray.asp

> gets this response

> Microsoft VBScript compilation error '800a0400'

> Expected statement

> /asp/passArray.asp, line 10

> Function ReturnArray() As Variant
> -----------------------^

> So what am I doing wrong? Sorry I didn't try out the other
> suggestions, but at this point feeling much more comfortable with copy
> & paste type solutions.

> Thanks,

> Marty



Tue, 27 Jul 2004 14:21:11 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. HELP: Passing an Array back in a function

2. Passing array back from Sub/Function

3. HELP: Using an Array passed back from a function

4. Passing Array to Session Variable Then back to Array

5. Passing a variant array from asp to a cls and back

6. VB5: Passing Arrays to a DLL and back

7. Passing array from ASP (VBScript) to DLL and back

8. Passing arrays back and forth from COM

9. Passing an array back from a class

10. how to get value passed back from function..

11. Passing recordset back from function

12. Passing recordset back from function

 

 
Powered by phpBB® Forum Software