限制类模块中的集合中的类型 [英] Restrict type in a Collection inside a class module

查看:104
本文介绍了限制类模块中的集合中的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类模块中的集合。我想限制addable对象类型到这个集合,即集合应该只接受一个给定类型的对象,没有别的。

I have a collection inside a class module. I'd like to restrict the object type that is "addable" to this collection, i.e. collection should only ever accept objects of one given type and nothing else.

有没有办法强制添加到集合中的对象类型?

Is there any way to enforce the type of objects added to a collection?

从我可以知道,没有内置的方法来做到这一点。是解决方案然后使这个集合私有,并为通常可访问的集合的方法构建包装函数,即添加删除 Item Count

From what I can tell, there is no built-in way to do this. Is the solution then to make this collection private, and build wrapper functions for the methods usually accessible for Collections, i.e. Add, Remove, Item, and Count?

有点怨恨不得不写3个包装函数,不添加任何功能,只是能够添加一些类型强制执行到添加方法。但是如果这是唯一的方式,那么这是唯一的方式。

I kinda hate having to write 3 wrapper functions that add no functionality, just to be able to add some type enforcement to the Add method. But if that's the only way, then that's the only way.

推荐答案

我喜欢Rob van Gelder的示例,指向by @jtolle,但为什么我应该满足于制作一个自定义集合类,只能接受一个特定的对象类型(例如 People ),永远?正如@jtolle指出,这是超级烦人。

This is what I did. I liked Rob van Gelder's example, as pointed to by @jtolle, but why should I be content with making a "custom collection class" that will only accept one specific object type (e.g. People), forever? As @jtolle points out, this is super annoying.

相反,我概括了这个想法,并创建了一个名为 UniformCollection 的类,可以包含任何数据类型只要所有项目在 UniformCollection 的任何给定实例中都是相同类型。

Instead, I generalized the idea and made a new class called UniformCollection that can contain any data type -- as long as all items are of the same type in any given instance of UniformCollection.

我添加了一个私有Variant,它是 UniformCollection 可以包含的给定实例的数据类型的占位符。

I added a private Variant that is a placeholder for the data type that a given instance of UniformCollection can contain.

Private mvarPrototype As Variant

在创建 UniformCollection 的实例之后,在使用它之前,必须通过指定它包含的数据类型来初始化它。

After making an instance of UniformCollection and before using it, it must be initialized by specifying which data type it will contain.

Public Sub Initialize(Prototype As Variant)
    If VarType(Prototype) = vbEmpty Or VarType(Prototype) = vbNull Then
        Err.Raise Number:=ERR__CANT_INITIALIZE, _
            Source:=TypeName(Me), _
            Description:=ErrorDescription(ERR__CANT_INITIALIZE) & _
                TypeName(Prototype)
    End If
    ' Clear anything already in collection.
    Set mUniformCollection = New Collection
    If VarType(Prototype) = vbObject Or VarType(Prototype) = vbDataObject Then
        ' It's an object. Need Set.
        Set mvarPrototype = Prototype
    Else
        ' It's not an object.
        mvarPrototype = Prototype
    End If
    ' Collection will now accept only items of same type as Prototype.
End Sub

Add方法将只接受相同类型的新项目作为原型(对象或原始变量...尚未使用UDT测试)。

The Add method will then only accept new items that are of the same type as Prototype (be it an object or a primitive variable... haven't tested with UDTs yet).

Public Sub Add(NewItem As Variant)
    If VarType(mvarPrototype) = vbEmpty Then
        Err.Raise Number:=ERR__NOT_INITIALIZED, _
            Source:=TypeName(Me), _
            Description:=ErrorDescription(ERR__NOT_INITIALIZED)
    ElseIf Not TypeName(NewItem) = TypeName(mvarPrototype) Then
        Err.Raise Number:=ERR__INVALID_TYPE, _
            Source:=TypeName(Me), _
            Description:=ErrorDescription(ERR__INVALID_TYPE) & _
                TypeName(mvarPrototype) & "."
    Else
        ' Object is of correct type. Accept it.
        ' Do nothing.
    End If

    mUniformCollection.Add NewItem

End Sub

其余几乎与示例中的相同(加上一些错误处理)。太糟糕了RvG没有去全方位!更糟糕的是,微软不包括这种东西作为一个内置的功能...

The rest is pretty much the same as in the example (plus some error handling). Too bad RvG didn't go the whole way! Even more too bad that Microsoft didn't include this kind of thing as a built-in feature...

这篇关于限制类模块中的集合中的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆