By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,795 Members | 1,216 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,795 IT Pros & Developers. It's quick & easy.

Returning an array by reference from a function

P: n/a
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?

cheers,
Krackers
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
> How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?


Do you mean you want your function to create an array out of thin air
and then give you access to it from outside of the function? You can't
do that; things declared inside of a function or sub are local to that
procedure. Besides, if you could do that, what would that mean the next
time the function was called? Anyway, you could declare an array in the
(General)(Declarations) section of a form (making it have form-wide
scope) or as Public in a BAS module (making it have project wide scope)
and then stuff values into it from within your function (and maybe ReDim
it if necessary). Other routines could then pick up the values by simply
referencing the "global" array variable directly.

Rick - MVP

Jul 17 '05 #2

P: n/a
On 16 Jun 2004 08:07:13 -0700, Krackers <kr*******@hotmail.com> wrote:
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?

cheers,
Krackers

There is no way to return an array ByRef in Visual Basic. You can
(almost) always design code so this isn't needed. Dimension an array with
the proper Scope. Instead of returning an array ByRef, the function
manipulates an array with a higher Scope (ei outside the function) which
is then accessible outside the function.

The only situation where you might want to fiddle with array pointers is
when the array is very large, in a Class, and you need to access it from
outside the Class. This is because you can not declare a Public array in
a Class. In this situation, you can copy the pointer of the array in the
Class to a dummy array variable outside the Class. See example:

http://www.planetsourcecode.com/vb/s...52009&lngWId=1

If the array is not in a Class, or is not very large, then efficient code
can be designed such that array pointers aren't necessary.
-korejwa
Jul 17 '05 #3

P: n/a
Rick,

The array would be held in a class, it's scope is module level. the
function is a function of the class and I want it to return a
reference to the module level array.

the code looks something like this:

'<class arrayHolder - Start>

private mArray()

public function rtnArray() as variant()
'code to do some formating to the array
rtnArray = mArray
end function

'<class arrayHolder - end>

I then want any changes outside the class 'arrayHolder' to affect the
array in arrayHolder (mArray). To make the 'mArray' public scope means
I wouldn't be able to run code before it was accessed because any
class would be able to access it directly. If returning a variable
from a function is by value only then I'll have to make the array
public or use a collection instead of an array (collections always
pass by reference since they are an object) but this would mean a fair
amount of code rewriting. If there is a way of making the return
variable of the function by reference it would save me a lot of time.

cheers,
Krackers
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<Zu********************@comcast.com>...
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?


Do you mean you want your function to create an array out of thin air
and then give you access to it from outside of the function? You can't
do that; things declared inside of a function or sub are local to that
procedure. Besides, if you could do that, what would that mean the next
time the function was called? Anyway, you could declare an array in the
(General)(Declarations) section of a form (making it have form-wide
scope) or as Public in a BAS module (making it have project wide scope)
and then stuff values into it from within your function (and maybe ReDim
it if necessary). Other routines could then pick up the values by simply
referencing the "global" array variable directly.

Rick - MVP

Jul 17 '05 #4

P: n/a
Rick,

The array would be held in a class, it's scope is module level. the
function is a function of the class and I want it to return a
reference to the module level array.

the code looks something like this:

'<class arrayHolder - Start>

private mArray()

public function rtnArray() as variant()
'code to do some formating to the array
rtnArray = mArray
end function

'<class arrayHolder - end>

I then want any changes outside the class 'arrayHolder' to affect the
array in arrayHolder (mArray). To make the 'mArray' public scope means
I wouldn't be able to run code before it was accessed because any
class would be able to access it directly. If returning a variable
from a function is by value only then I'll have to make the array
public or use a collection instead of an array (collections always
pass by reference since they are an object) but this would mean a fair
amount of code rewriting. If there is a way of making the return
variable of the function by reference it would save me a lot of time.

cheers,
Krackers
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<Zu********************@comcast.com>...
How do you write a function which returns a reference to an array. I
can only get a function to return a copy of the array itself. I've had
a look at some other threads in this group an the return value of a
function acts like 'by Val' returning the value only (except for
objects) can you make it return a reference instead?


Do you mean you want your function to create an array out of thin air
and then give you access to it from outside of the function? You can't
do that; things declared inside of a function or sub are local to that
procedure. Besides, if you could do that, what would that mean the next
time the function was called? Anyway, you could declare an array in the
(General)(Declarations) section of a form (making it have form-wide
scope) or as Public in a BAS module (making it have project wide scope)
and then stuff values into it from within your function (and maybe ReDim
it if necessary). Other routines could then pick up the values by simply
referencing the "global" array variable directly.

Rick - MVP

Jul 17 '05 #5

P: n/a

"Krackers" <kr*******@hotmail.com> wrote in message
news:c0**************************@posting.google.c om...
Rick,

The array would be held in a class, it's scope is module level. the
function is a function of the class and I want it to return a
reference to the module level array.

the code looks something like this:

'<class arrayHolder - Start>

private mArray()

public function rtnArray() as variant()
'code to do some formating to the array
rtnArray = mArray
end function

'<class arrayHolder - end>

I then want any changes outside the class 'arrayHolder' to affect the
array in arrayHolder (mArray).


You might be able to get the result you need by using an indexed
property:

Public Property Get ArrayItem(ByVal Index) As Variant
ArrayItem = mArray(Index)
End Property

Public Property Let ArrayItem(ByVal Index, ByVal RHS As Variant)
mArray(Index) = RHS
End Property

Code outside of the class can then manipulate data using:
With oArrayCls
x = .ArrayItem(4)
.ArrayItem(5) = 7
End With

and so on.
Jul 17 '05 #6

P: 1
GRT
I'm certainly no expert when it comes to VB programming, but returning a reference to an array is common in almost all programming languages. You should not have to compromise quality by using gloabl variables for arrays.

Here is a program I wrote which I believe returns a reference to an array.

Expand|Select|Wrap|Line Numbers
  1. Sub start()
  2.     Dim myArray As Variant
  3.     myArray = createArray(Application.Documents(1))
  4.     MsgBox myArray(5, 5)
  5. End Sub
  6.  
  7. Function createArray(aDocument As Document)
  8.  
  9.     Dim aTable As Table
  10.     Set aTable = aDocument.Tables(aDocument.Tables.Count - 1)
  11.  
  12.     Dim x As Integer
  13.     Dim y As Integer
  14.  
  15.     x = aTable.Columns.Count
  16.     y = aTable.Rows.Count
  17.  
  18.     ReDim tableArray(x, y) As Object
  19.  
  20.     For i = 1 To x
  21.         For i2 = 1 To y
  22.             Set tableArray(i, i2) = aTable.Cell(i2, i)
  23.         Next
  24.     Next
  25.  
  26.     createArray = tableArray
  27.  
  28. End Function
Im not sure how VB works, but im assuming it makes use of both a heap and a stack like Java. If this is the case the array would exsist in the heap, and a reference to it in the stack, there is no reason we cant point to the heap object through many references.
Jun 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.