2

Note sure if 'wrapping' is the right terminology. Essentially, I'm building a class that has another class as a private member, and I want to expose some (not all) of the child class's functions in the parent's interface. To make the example simple, I'll use a Collection as an example:

'MyClass
Private m_ClsMyCollection As Collection

...

'Expose Collection.Count
Public Function Count() As Long
    Count = m_ClsMyCollection.Count
End Sub

Simple enough, but I'm hitting a bit of a sticking point when exposing methods with optional parameters. For example, Collection.Add is declared as

Collection.Add(Item, [Key], [Before], [After])

I'm not sure how to wrap this. Is it safe to simply do:

Public Sub Add(Item, Optional Key, Optional Before, Optional After)
    m_ClsMyCollection.Add Item, Key, Before, After
End Sub

Presumably if the optional parameters are missing, it will pass Nothing to those parameters in m_ClsMyCollection.Add, but I suspect that passing Nothing is not equivilent to not passing an argument at all.

The alternative seems to be to check IsMissing on each arg and write a pass for every possible combination of arguments, which seems mad:

Public Sub Add(Item, Optional Key, Optional Before, Optional After)
    If IsMissing(Key) And IsMissing(Before) And IsMissing(After) Then
        m_ClsMyCollection.Add Item:=Item
    ElseIf IsMissing(Key) And IsMissing(Before) Then
        m_ClsMyCollection.Add Item:=Item, After:=After
    ElseIf IsMissing(Key) And IsMissing(After) Then
        m_ClsMyCollection.Add Item:=Item, Before:=Before
    ...
End Sub

The amount of combinations increases exponentially with the number of optional args - even with just 3, I need to check 8 cases! Is this necessary? Is there a better way?

1 Answer 1

2

I was able to test this by doing the following:

Public Sub Foo()
    Wrapper
End Sub

Public Sub Wrapper(Optional MyArg)
    Wrapped MyArg
End Sub

Public Sub Wrapped(Optional MyArg)
    Debug.Print IsMissing(MyArg)
End Sub

This outputs True, proving it's not necessary to check if every optional argument is passed - in the case above, m_ClsMyCollection.Add Item, Key, Before, After is sufficient regardless of which arguments were passed.

Sign up to request clarification or add additional context in comments.

1 Comment

For completeness: What happens in Kai's example is when Wrapper calls Wrapped MyArg, MyArg has a value of Missing. So, even though it is actually passed to Wrapped, it has a value of Missing so, IsMissing evaluates to true. (Making this a completely safe thing to do.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.