Help with paramarray

J

Jeremy

I have a class with a method that essentially creates a new object of
the same type with the same contents. Since I may want to omit one or
more of the source instance's contents, I have a paramarray argument in
there, and basically, if one of the items exists in the passed array,
it gets skipped and not copied.

Now, I've gone from specifying the excluded items as a list in the
arguments to creating an array of excluded items. I figured that this
would work the same way, but for some reason, the method is getting an
array where the first element is also an array (which is the element I
want).

Here is my crappy code:

Public Function Replicate( _
ParamArray Exclude() As Variant) As Factors

Dim fct As New Factors
Dim lngIndex As Integer
Dim varNames As Variant
Dim varFactors As Variant
Dim varIgnore As Variant

varNames = pDict.Keys
varFactors = pDict.Items
varIgnore = Exclude
For lngIndex = 0 To pDict.Count - 1

If Not IsElementOf(CStr(varNames(lngIndex)), varIgnore) Then

fct.Add CStr(varNames(lngIndex)), CCur(varFactors(lngIndex))

End If

Next lngIndex

Set Replicate = fct

End Function

Any idea what I'm doing wrong?
 
J

Jeremy

To clarify what the problem is, here's what's happening:

if I call Factors.Replicate("x","y","z"), the paramarray picks up the
correct number of elements. Then I set varIgnore equal to the
paramarray, and if the key is equal to one of the elements of
varIgnore, it's skipped and not copied.

However, if I set varParameters = {"x","y","z"}, then call
Factors.Replicate(varParameters), varParameters is added to the
paramarray instead of being passed AS the paramarray. Thus,
varIgnore(0) = varParameters, and the method can no longer find the
elements of the array that I actually intended as arguments.

MSDN says paramarray should work the same way regardless of whether
you're using a list of arguments or passing it an array, so I have no
idea why this is happening. And unfortunately, it's essential that I
use the array as an argument. I can have the procedure looking at the
elements of varIgnore(0), but that doesn't change the fact that it
shouldn't be storing the array inside varIgnore(0) in the first place.


And really, it makes sense that it would function this way, because it
seems like it should be taking a set of arguments and storing each one
as an element of the paramarray. Is the MSDN page in error, or (more
likely) am I doing something wrong?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top