Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Passing a function as a parameter

Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Passing a function as a parameter

the addressof operator probably is what you want.

the description in vba help:

A unary operator that causes the address of the procedure it precedes to be
passed to an API procedure that expects a function pointer at that position
in the -- argument list.



--

Regards,

Tom Ogilvy



"Scott" wrote in message
oups.com...
Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Passing a function as a parameter

"Tom Ogilvy" wrote in message
the addressof operator probably is what you want.


It isn't clear whether the original poster is writing the sort code in
VB/VBA or in C/C++. If the code is written in C/C++, then the AddressOf
operator will be useful. However, if the code is written in VB/VBA, the
AddressOf operator will be useless. You can surely use it to obtain the
address of a function, but there is no way in VB/VBA to execute the function
pointed to by the value returned by AddressOf.

I have a qsort procedure for arrays of simple variable types (e.g., Longs,
Strings, etc) at www.cpearson.com/excel/QSort.htm and a procedure for
arrays of objects of any type at
http://www.cpearson.com/excel/Sortin...sOfObjects.htm . Both support
user-defined comparison functions, but the functions must be named
"QSortCompare" (for arrays of simple variables) or "QSortObjectCompare" (for
arrays of Objects).

The only other way to do it would be to put the comparison function(s) in a
Class module and use CallByName to call the appropriate comparison
procedure, but this would likely introduce an excessive amount of overhead
if the arrays are large.

I use AddressOf in VBA in a Windows Timer Manager program I'm writing, but
all I do with the address is pass it forward to the SetTimer API function.
If you are staying within VB/VBA, there is nothing useful you can do with
AddressOf.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Tom Ogilvy" wrote in message
...
the addressof operator probably is what you want.

the description in vba help:

A unary operator that causes the address of the procedure it precedes to
be passed to an API procedure that expects a function pointer at that
position in the -- argument list.



--

Regards,

Tom Ogilvy



"Scott" wrote in message
oups.com...
Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 87
Default Passing a function as a parameter

Thanks for the responses.

Your sorting routines sound like what I was looking for Chip. I will
admit I was trying to figure out how to get the AddressOf to work in
VBA even though the references said it wouldn't. The other schemes for
passing functions did seem to have a lot of overhead for something that
would be frequently called for most sorts.

Some day I'll delve into the API world, but not yet.

Thanks,
Scott

Chip Pearson wrote:
"Tom Ogilvy" wrote in message
the addressof operator probably is what you want.


It isn't clear whether the original poster is writing the sort code in
VB/VBA or in C/C++. If the code is written in C/C++, then the AddressOf
operator will be useful. However, if the code is written in VB/VBA, the
AddressOf operator will be useless. You can surely use it to obtain the
address of a function, but there is no way in VB/VBA to execute the function
pointed to by the value returned by AddressOf.

I have a qsort procedure for arrays of simple variable types (e.g., Longs,
Strings, etc) at www.cpearson.com/excel/QSort.htm and a procedure for
arrays of objects of any type at
http://www.cpearson.com/excel/Sortin...sOfObjects.htm . Both support
user-defined comparison functions, but the functions must be named
"QSortCompare" (for arrays of simple variables) or "QSortObjectCompare" (for
arrays of Objects).

The only other way to do it would be to put the comparison function(s) in a
Class module and use CallByName to call the appropriate comparison
procedure, but this would likely introduce an excessive amount of overhead
if the arrays are large.

I use AddressOf in VBA in a Windows Timer Manager program I'm writing, but
all I do with the address is pass it forward to the SetTimer API function.
If you are staying within VB/VBA, there is nothing useful you can do with
AddressOf.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Tom Ogilvy" wrote in message
...
the addressof operator probably is what you want.

the description in vba help:

A unary operator that causes the address of the procedure it precedes to
be passed to an API procedure that expects a function pointer at that
position in the -- argument list.



--

Regards,

Tom Ogilvy



"Scott" wrote in message
oups.com...
Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 41
Default Passing a function as a parameter

The following works for me:

Sub PassFunctionAsParameter()

dim strFunctionName as string
Dim FunctionArg1 as variant (or whatever)
Dim FunctionArg2 as variant (or whatever)
Dim Result as double (or whatever datatype)

strFunctionName = "MyFunction"

Result = Run(strFunctionName, FunctionArg1, FunctionArg2)

End sub

Function MyFunction(FunctionArg1 as variant, FunctionArg2 as variant) as
double 'or whatever datatype you want

' do stuff

MyFunction = whateverstuffyouwanthere

End function


I've found this extremely useful where I store function names in an array,
and then call the functions when needed. If different functions have
different number of arguments, then just add optional arguments to each
function so that they all have the same number of required + optional
arguments. Make sure you test for empty optional arguments.


This may or may not solve the problem you had.


"Scott" wrote:

Thanks for the responses.

Your sorting routines sound like what I was looking for Chip. I will
admit I was trying to figure out how to get the AddressOf to work in
VBA even though the references said it wouldn't. The other schemes for
passing functions did seem to have a lot of overhead for something that
would be frequently called for most sorts.

Some day I'll delve into the API world, but not yet.

Thanks,
Scott

Chip Pearson wrote:
"Tom Ogilvy" wrote in message
the addressof operator probably is what you want.


It isn't clear whether the original poster is writing the sort code in
VB/VBA or in C/C++. If the code is written in C/C++, then the AddressOf
operator will be useful. However, if the code is written in VB/VBA, the
AddressOf operator will be useless. You can surely use it to obtain the
address of a function, but there is no way in VB/VBA to execute the function
pointed to by the value returned by AddressOf.

I have a qsort procedure for arrays of simple variable types (e.g., Longs,
Strings, etc) at www.cpearson.com/excel/QSort.htm and a procedure for
arrays of objects of any type at
http://www.cpearson.com/excel/Sortin...sOfObjects.htm . Both support
user-defined comparison functions, but the functions must be named
"QSortCompare" (for arrays of simple variables) or "QSortObjectCompare" (for
arrays of Objects).

The only other way to do it would be to put the comparison function(s) in a
Class module and use CallByName to call the appropriate comparison
procedure, but this would likely introduce an excessive amount of overhead
if the arrays are large.

I use AddressOf in VBA in a Windows Timer Manager program I'm writing, but
all I do with the address is pass it forward to the SetTimer API function.
If you are staying within VB/VBA, there is nothing useful you can do with
AddressOf.

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"Tom Ogilvy" wrote in message
...
the addressof operator probably is what you want.

the description in vba help:

A unary operator that causes the address of the procedure it precedes to
be passed to an API procedure that expects a function pointer at that
position in the -- argument list.



--

Regards,

Tom Ogilvy



"Scott" wrote in message
oups.com...
Hi,

I was wondering if it is possible to pass a function as a parameter.
(If you have a link and/or an example, that would be great.)

My intention is to write a C/C++ like qsort function, where I can pass
a reference to an array and the comparison function with it returning
the sorted array.

Thanks,
Scott





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing parameter to a ms query redf1re Excel Programming 6 February 7th 06 10:39 PM
Passing a Sub's name as parameter Stefi Excel Programming 7 June 20th 05 08:48 PM
Passing parameter to a query Dwaine Horton[_3_] Excel Programming 6 April 26th 05 02:24 AM
?Passing argument/parameter just starting[_2_] Excel Programming 0 October 23rd 04 07:56 PM
?Passing argument/parameter just starting Excel Programming 1 October 23rd 04 04:23 PM


All times are GMT +1. The time now is 06:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"