473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Returning an array by reference from a function

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
6 13989
> 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
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
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
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

"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
GRT
1
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
10
by: Fraser Ross | last post by:
I need to know the syntax for writing a reference of an array. I haven't seen it done often. I have a class with a member array and I want a member function to return an reference to it. ...
3
by: Faustino Dina | last post by:
Hi, The following code is from an article published in Informit.com at http://www.informit.com/guides/content.asp?g=dotnet&seqNum=142. The problem is the author says it is not a good idea to...
2
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
9
by: josh | last post by:
Hi, I'm converting (for learning purpose) )a program from Java to C++ and I've a doubt: In Java every argument (variable and reference types) is passed and returned in functions by-value so if I...
11
by: jza | last post by:
Hello all, I am fairly new to c, coming from a Java background. I am working on a mathematical program and I have a function that needs to return a 2-d array. After some web searching, I have...
8
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.