Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is VBA.Collection.Count a method

The VBA Collection has 5 members, all of which are methods: Add, Count, Item, _NewEnum and Remove.

Of the 5 members, the Count method looks like it could/should be a getter rather than a method.

Is this a legacy of OOP/MS design from the early days of VB6? Did the designers of the Collection anticipate a time when Count might need to be a method, and designate Count as a method so that some future behavioral change wouldn't break the interface/COM?

EDIT As Jim Hewitt points out in the comments, the online documentation suggests that Count is a property, but Object Browser and the MIDL suggest otherwise.

Here's the documentation for VB6's Collection.Count Property

Returns a Long (long integer) containing the number of objects in a collection. Read-only.

But the VB6 Object Browser shows this: enter image description here

And in Excel 2016, Count sure looks lonely and like it wants to be a getter...

enter image description here

EDIT 22/Sep/16:

I referred back to Hardcore Visual Basic 5 by Bruce McKinney...

This doesn't exactly explain why Collection.Count is a method, but it does go some way to answering why I can't know why Collection.Count is a method:

On knowing the unknowable:

anything you think you know about the implementation of a Collection might be wrong for this version and probably will be wrong for the next version.

On Implementation details:

The Collection class provided with Visual Basic version 4 was a good 1 version. The Collection class provided with Visual Basic version 5 is not a good 2 version. Users offered many complaints and suggestions. None of them were implemented.

On Collections:

What is a Collection? You know its user interface, but you don’t really know what internal data structure makes it work. Normally, this isn’t a concern. Who cares how it works as long as it works? But, on second thought, the internal implementation is bound to affect the efficiency of operations. If some operations are less efficient than others, it might be nice to avoid the slow ones.

On Collection.Item:

Visual Basic could have made [Collection.Item] work [as a property]. (It was requested.)

like image 951
ThunderFrame Avatar asked Sep 20 '16 09:09

ThunderFrame


People also ask

What is the difference between array and collection in VBA?

Collections in VBA are objects that can store groups of related items, much like an array. Unlike arrays, a single collection can store items of different types because each item in a collection is stored as a Variant.

How do collections work in VBA?

Now, an Excel VBA Collection is a group of similar items. You can use built-in collections such as the Workbooks collection, which is a collection of all the open workbooks, in your VBA code. The Worksheets Collection, on the other hand contains all the worksheets in your workbook.

Are VBA collections zero based?

Basic collection operations: Add, access and remove items add(elem) method. The nth item in a collection can be accessed with coll(n) . Note that unlike in C and C++ and many other programming languages, the element-index of a VBA-collection is not zero based.

What is count in VB?

Count property (VBA)Returns a Long (long integer) containing the number of objects in a collection.


1 Answers

As far as I can tell it actually is a method:

[
  odl,
  uuid(A4C46780-499F-101B-BB78-00AA00383CBB),
  helpcontext(0x000f7886),
  hidden,
  dual,
  oleautomation
]
interface _Collection : IDispatch {
    [id(00000000), helpcontext(0x000f7903)]
    HRESULT Item(
                    [in] VARIANT* Index, 
                    [out, retval] VARIANT* pvarRet);
    [id(0x00000001), helpcontext(0x000f7901)]
    HRESULT Add(
                    [in] VARIANT* Item, 
                    [in, optional] VARIANT* Key, 
                    [in, optional] VARIANT* Before, 
                    [in, optional] VARIANT* After);
    [id(0x00000002), helpcontext(0x000f7902)]
    HRESULT Count([out, retval] long* pi4);
    [id(0x00000003), helpcontext(0x000f7904)]
    HRESULT Remove([in] VARIANT* Index);
    [id(0xfffffffc)]
    HRESULT _NewEnum([out, retval] IUnknown** ppunk);
};

The "why" escapes me though.

like image 107
Bob77 Avatar answered Oct 16 '22 05:10

Bob77