defaultcollelem

Description

Allows for optimization of code.

Allowed on

Property, members in dispinterfaces and interfaces.

Comments

In Visual Basic for Applications (VBA5.0), "foo!bar" is normally syntactic shorthand for foo.defaultprop("bar"). Because such a call is significantly slower than accessing a data member of foo directly, an optimization has been added in which the compiler looks for a member named "bar" on the type of foo. If such a member is found and flagged as an accessor function for an element of the default collection, a call is generated to that member function. To allow vendors to produce object servers that will be optimized in this way, the member flag should be documented.

Because this optimization searches the type of item that precedes the ‘!', it will optimize calls of the form MyForm!bar only if MyForm has a member named "bar," and it will optimize MyForm.Controls!bar only if the return type of Controls has a member named bar. Even though MyForm!bar and MyForm.Controls!bar both would normally generate the same calls to the object server, optimizing these two forms requires that the object server add the bar method in both places.

Use of [defaultcollitem] must be consistent for a property. For example, if it is present on a Get, it must also be present on a Put.

Flags

FUNCFLAG_FDEFAULTCOLLELEM

VARFLAG_FDEFAULTCOLLELEM

Example

A form has a button on it named Button1. User code can access the button using property syntax or ! syntax, as shown below.

Sub Test()
    Dim f As Form1
    Dim b1 As Button
    Dim b2 As Button

    Set f = Form1

    Set b1 = f.Button1        ' Property syntax
    Set b = f!Button1        ' ! syntax
End Sub

To use the property syntax and the ! syntax properly, see the form in the type information below.

    [    odl, 
        dual,
        uuid(1e196b20-1f3c-1096-996b-00dd010ef676),
        helpstring("This is IForm"),
        restricted
    ]
    interface IForm1: IForm
    {
        [propget, defaultcollelem]
        HRESULT Button1([out, retval] Button *Value);
    }