473,835 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14040
> 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)(Decla rations) 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*******@hotm ail.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******* *************@c omcast.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)(Decla rations) 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******* *************@c omcast.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)(Decla rations) 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*******@hotm ail.com> wrote in message
news:c0******** *************** ***@posting.goo gle.com...
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 New Member
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
2877
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 returns a reference to the found item.
7
7307
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 array e.g: array *a; a = function(...)
5
3119
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 function below is returning a pointer to pointers who are GUID. I am trying to write a wrapper to use in my VB code and what I would prefer to do is be able to return an array of GUID. I remember (not sure) that the concept of arrays does not really...
10
10302
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. Returning a pointer to the first element might do but I want to do what I've said. Fraser.
3
2705
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 return an array as a property because it will return a copy of the array instead a reference to it. How can I force the property to return a reference to the array? Is it only a feature of arrays? I hope normal class objects (including collections)...
2
7155
by: Tany | last post by:
How can I declare function returning array of Integer pointers . Please help !!
9
1888
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 want to create inside that function a matrix (i.e. int a) and than passing the reference to it I can simply returning that value: in main()
11
2057
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 determined that in c, it is better to just pass the array in the function. Since arrays are passed by reference, I expected for the updated array to be passed back with the correct values filled in, however, only the first row of the array is returned...
8
2223
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
9803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10808
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10520
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4433
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3088
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.