LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 968
Default Excel - DotNet - Variant Arrays as Params

Hi Paul,

To explain whats happening: A variant containing an array (or a range) is
not the same as an array of variants.

If you want to access the parameter as a variant array just assign it to a
variant.

Public Function GoodBadFunction(avVariant As Variant) As Variant
Dim vVar As Variant
Debug.Print TypeName(avVariant)
vVar = avVariant
Debug.Print TypeName(vVar)
GoodBadFunction = vVar
Debug.Print TypeName(GoodBadFunction)
End Function

What happens is that when a variant parameter is passed a range, the variant
is treated as containing a range,
and when that variant containing a range is assigned to another variant, the
range is converted to a variant containing an array of variants.
(the exact treatment differs slightly by VBA version).
However Excel will not let you pass a range to a variant array parameter
(avVariant() as variant).

This is also important for performance reasons:
converting a range to a variant containing an array of variants with a
single assignment statement is very efficient compared to reading the
elements of the range one--by-one,
but if you only want a few elements of the range (typically things like
binary search or excel functions like MATCH and VLOOKUP) then it is much
faster not to assign it to a variant but to handle it as a variant
containing a range.

I assume that using a paramarray involves each individual parameter being
handled internally as a variant which is then assigned to a single element
(as an array of variants) of the variant array that the function sees. so
the paramarray winds up as an array of variants each element of which can
contain an array.

Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com

"Charles Williams" wrote in message
...
It works OK if you use as variant rather than as variant(), and also

handles
non-range arguments.

Public Function GoodBadFunction(avVariant as Variant) as String
goodBadFunction= TypeName(avVariant)
End Function

regds
Charles
______________________
Decision Models
FastExcel Version 2 now available.
www.DecisionModels.com

"Paul S Bryant" wrote in message
om...
I agree that the elements in the Variant() array in the GoodFunction()
are indeed of type Range. This wouldn't help me pass data to DotNet
without the PIA, but it does confirm the thing I am immediately
interested in. That is, the Excel function bar can't pass parameters
to functions requiring variant arrays unless you use the ParamArray
modifier.


"Tom Ogilvy" wrote in message

...
I believe your interpretation of GoodFunction is incorrect. If you

test
the typename of the first element of avVariant, it will be Range. No
conversion has taken place. Replace the argument with a single

number,
and
as written you will get the same result.

=GoodFunction(21)


Your interpretation of BadFunction is correct in that you have a

mismatch
with argument type and the function is never entered.







 
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
Opening XLS file directly and getting params [email protected] Excel Discussion (Misc queries) 0 December 2nd 05 06:16 PM
OWC load add-ins programmatically in dotnet? ESmith Excel Discussion (Misc queries) 0 September 16th 05 07:36 PM
Variant to String Chip Pearson Excel Programming 1 September 3rd 03 03:10 PM
Excel Workbook opened twice with dotnet interop Steve Weixel Excel Programming 0 August 25th 03 11:37 PM
DLLs and VBA: Who free's a variant? Rob Bovey Excel Programming 0 August 5th 03 09:26 PM


All times are GMT +1. The time now is 11:30 AM.

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

About Us

"It's about Microsoft Excel"