vb6等同于list< someclass> [英] vb6 equivalent to list<someclass>

查看:70
本文介绍了vb6等同于list< someclass>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在(.net)的等价物

I want to know if exist a equivalent of (.net)

list<somefixedclass> 

在vb6

我知道vb6中存在存在的集合,但是它使用对象(变量)而不是特定对象.

I know that exist collection in vb6 but it uses object (variant) instead of a specific object.

谢谢.

推荐答案

在VB 6中没有直接等效于VB.NET中的通用List<T>的功能.但是,VB 6中有Collection之类的东西,它提供了相似的功能.主要区别在于VB 6 Collection不是强类型,这意味着所有对象都以Variants的形式存储在集合中.在某些情况下,这可能是有益的,因为它使您可以在同一集合中存储许多不同类型的数据,并且实际上,VB在内部使用此对象.从类中检索Collection和向上转换的对象很容易,但是您几乎无能为力.在VB运行时中无法实现强类型的集合.

There is no direct equivalent in VB 6 to the generic List<T> found in VB.NET. However, there is such a thing as a Collection in VB 6, which provides similar functionality. The primary difference is that a VB 6 Collection is not strongly-typed, which means that all objects are stored as Variants in the collection. In some cases, this can be beneficial, because it allows you to store many different types of data in the same collection, and in fact, VB uses this object internally. It's easy enough to use a Collection and up-cast objects as they are retrieved from the class, but there's little you can do. It's not possible to implement strongly-typed collections in the VB runtime.

话虽这么说,但是您可以实现一种解决方法.与在引入泛型之前在早期版本的VB.NET中实现集合的方式类似,您可以将Collection包装在一个类中,其中对内部的唯一访问是通过从中公开的方法进行的班.这种设计模式通常称为自定义集合" .

That being said, there is a workaround you can implement. Similarly to how collections were implemented in early versions of VB.NET before generics were introduced, you can wrap the Collection in a class, where the only access to the internal Collection is through methods that you expose from this class. This design pattern is commonly referred to as a "custom collection".

这确实具有自动处理强制转换的好处,并且减轻了代码使用者不必记住诸如此类的实现细节的麻烦.它考虑了(极有可能)您在运行时循环遍历一个仅包含一种类型的对象的集合的可能性,但是偶然添加了第二种不兼容的对象类型,这会导致您的代码引发异常.当然,缺点是您必须以自定义集合上的公共方法的形式重新实现Collection对象已经提供的大多数功能.

This does have the benefit of automatically handling casting, and alleviates the consumers of your code from having to remember to mind implementation details like this. It takes care of the (all too likely) possibility that you'll be looping through a collection at run-time that is only supposed to contain one type of object, but accidentally had a second, incompatible type of object added that causes your code to throw an exception. Of course, the disadvantage is that you have to re-implement most of the functionality already provided by the Collection object yourself, in the form of public methods on your custom collection.

以下是您可能如何处理的示例:

Here's an example of how you might go about that:

Public Class CustomerCollection

    ''#Internal collection, exposed by this class
    Private m_Customers As Collection

    Private Sub Class_Initialize()
        ''#Set up the internal collection
        Set m_Customers = New Collection
    End Sub

    Public Sub Add(ByVal cust as Customer, Optional ByVal key as String)
        ''#Add the Customer object to the internal collection
        If IsMissing(key) Then
            m_Customers.Add cust
        Else
            m_Customers.Add cust, key
        End If
    End Sub

    Public Property Get Count() As Integer
        ''#Return the number of objects in the internal collection
        Count = m_Customers.Count
    End Property

    Public Sub Remove(ByVal index As Variant)
        ''#Remove the specified object from the internal collection,
        ''# either by its index or its key
        m_Customers.Remove index
    End Sub

    Public Function Item(ByVal index As Variant) as Customer
        ''#Return the specified object from the internal collection,
        ''# either by its index or its key
        Set Item = m_Customers.Item(index)
    End Function

    Public Sub Clear()
        ''#Removes all objects from the internal collection
        Set m_Customers = New Collection
    End Sub

End Class

请注意,为了将自定义集合的Item属性设置为集合的默认方法(如内置的Collection对象),您需要在VB 6 IDE中执行以下步骤:

Note that in order to set the custom collection's Item property as the collection's default method (like the built-in Collection object), you need to follow these steps in the VB 6 IDE:

  1. 从工具"菜单中,单击过程属性"

  1. From the "Tools" menu, click "Procedure Attributes"

从名称"组合框中选择自定义类的名称.

Select the name of your custom class from the "Name" combo box.

出现对话框时,单击高级"按钮.

When the dialog appears, click the "Advanced" button.

在过程ID"组合框中选择(默认)"项.

Select the "(Default)" item in the "Procedure ID" combo box.

单击确定"


如果您还想允许使用For Each语法枚举您的自定义类(也类似于内置的Collection对象),则可以向您的自定义类添加NewEnum函数:


If you'd also like to allow enumeration of your custom class using the For Each syntax (also like the built-in Collection object), you can add a NewEnum function to your custom class:

Public Property Get NewEnum() As IUnknown
    ''#Provides support for enumeration using For Each
    Set NewEnum = m_Customers.[_NewEnum]
End Property

完成此操作后,您需要指示VB使用此属性:

Once you've done that, you need to instruct VB to use this property:

  1. 像以前一样,从工具"菜单中打开过程属性"对话框

  1. As before, open the "Procedure Attributes" dialog from the "Tools" menu

从名称"组合框中选择自定义类的名称.

Select the name of your custom class from the "Name" combo box.

出现对话框时,单击高级"按钮.

When the dialog appears, click the "Advanced" button.

在过程ID"组合框中键入数字"-4".

Type the number "-4" in the "Procedure ID" combo box.

单击确定"

这篇关于vb6等同于list&lt; someclass&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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