469,271 Members | 1,776 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Returning an array from a function

I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples suggest
it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String, RequiredResultSetName
as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with the
line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.
Jul 17 '05 #1
6 20884
Try having the function return a variant instead of a string()

--
Stéphane Richard
"Ada World" Webmaster
http://www.adaworld.com
"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message
news:LM*****************@news-binary.blueyonder.co.uk...
I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples suggest it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String, RequiredResultSetName as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with the line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.

Jul 17 '05 #2
OK, tried that BUT without any success - I must have missed something
somewhere else if this is working for you.

Thanks and regards

--Jim.
"Stephane Richard" <st**************@verizon.net> wrote in message
news:On******************@nwrdny01.gnilink.net...
Try having the function return a variant instead of a string()

--
Stéphane Richard
"Ada World" Webmaster
http://www.adaworld.com
"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message
news:LM*****************@news-binary.blueyonder.co.uk...
I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples

suggest
it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String,

RequiredResultSetName
as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with

the
line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.


Jul 17 '05 #3
Just send a reference to the array to a sub. For example:

Public Sub calculateData(ByRef myData)
myData(5) = 1
end sub

sub Main()
Dim myData(0 to 100) as integer
Call calculateData(ByRef myData)
Debug.print myData(5)
end sub

"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message news:<LM*****************@news-binary.blueyonder.co.uk>...
I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples suggest
it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String, RequiredResultSetName
as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with the
line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.

Jul 17 '05 #4
This seems to work OK - thanks!

Regards

--Jim.

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Just send a reference to the array to a sub. For example:

Public Sub calculateData(ByRef myData)
myData(5) = 1
end sub

sub Main()
Dim myData(0 to 100) as integer
Call calculateData(ByRef myData)
Debug.print myData(5)
end sub

"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message

news:<LM*****************@news-binary.blueyonder.co.uk>...
I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples suggest it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String, RequiredResultSetName as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with the line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.

Jul 17 '05 #5
Private Sub Command1_Click()

Dim x() As String

x = DoSomething("hello", "world")

Print x(0)
Print x(1)

End Sub
Private Function DoSomething(p1 As String, p2 As String) As String()

Dim buff() As String

ReDim buff(0 To 1) As String

buff(0) = p1
buff(1) = p2

DoSomething = buff

End Function

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message
news:LM*****************@news-binary.blueyonder.co.uk...
: I am sure I have had this problem before and can't remember what the fix
: is...
:
: The documentation says that an array can be returned from a function
: provided you do the following:-
:
: 1. Declare the function with an explicit return type - the examples
suggest
: it also needs the parentheses to denote a returned array thus:
:
: Private Function GetResultArray(RawResults as String,
RequiredResultSetName
: as String) as String()
:
: 2. Return the array (without parentheses) as part of the Exit Function
: statement thus:
:
: Exit Function MyTempArray
:
: Why do I always get a syntax error on the Exit Function statement, with
the
: line compiler complaining it is expecting end of statement after Exit
: Function?
:
: Any help appreciated...
:
: Regards
:
: --Jim.
:
:
Jul 17 '05 #6

"Jim SUTTON" <ji**********@bradleysutton.net> wrote in message
news:LM*****************@news-binary.blueyonder.co.uk...
I am sure I have had this problem before and can't remember what the fix
is...

The documentation says that an array can be returned from a function
provided you do the following:-

1. Declare the function with an explicit return type - the examples suggest
it also needs the parentheses to denote a returned array thus:

Private Function GetResultArray(RawResults as String, RequiredResultSetName
as String) as String()

2. Return the array (without parentheses) as part of the Exit Function
statement thus:

Exit Function MyTempArray

Why do I always get a syntax error on the Exit Function statement, with the
line compiler complaining it is expecting end of statement after Exit
Function?

Any help appreciated...

Regards

--Jim.

You need to exit with:
FunctionName = MyTempArray
Exit Function ' (or End Function)

I never heard of putting a return value on the same line as Exit Function.
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by shyam | last post: by
14 posts views Thread by Protoman | last post: by
reply views Thread by DWalker | last post: by
13 posts views Thread by Karl Groves | last post: by
9 posts views Thread by =?Utf-8?B?RGFya21hbg==?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.