Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
passing parameter to a ms query | Excel Programming | |||
Passing a Sub's name as parameter | Excel Programming | |||
Passing parameter to a query | Excel Programming | |||
?Passing argument/parameter | Excel Programming | |||
?Passing argument/parameter | Excel Programming |