1
22
在VB 6中没有与泛型直接等效的
也就是说,有一个变通方案可以实现。与在引入泛型之前如何在vb.net的早期版本中实现集合类似,
你可以把
这确实有自动处理强制转换的好处,并且减少了代码的使用者必须记住这样的实现细节。它考虑了(极有可能)在运行时循环通过一个集合的可能性,该集合只包含一种类型的对象,但意外添加了第二种不兼容的对象类型,从而导致代码引发异常。当然,缺点是您必须重新实现
下面是一个例子,说明您可能会怎么做:
请注意,为了设置自定义集合的
完成后,需要指示vb使用此属性:
|
2
5
这是arraylist的实现。可以将它用作强类型类的基(显然不是通过继承,而是通过codygray答案中表示的组合),但如果不需要类型安全性,它比集合类要好得多。
|
3
2
编辑:如果Cody Gray的解决方案对您的需求来说太过庞大,您可以尝试“穷人列表”解决方案,如下所示:
当然,A
|
4
1
vb6是一种古老的语言。它不包含现代语言(C++、C语言、Java)中的模板类型。因此,您必须将对象作为变量存储在集合中,然后在稍后将其转换回对象类型。 |