简体   繁体   English

如何在vb.net中使用vb6的新集合

[英]how to use new collection of vb6 in vb.net

I am converting vb6 project to vb.net but have stuck on these 我将vb6项目转换为vb.net,但是卡在了这些项目上

Private mCol As Collection

Public Property Get NewEnum() As IUnknown
    'this property allows you to enumerate
    'this collection with the For...Each syntax
    Set NewEnum = mCol.[_NewEnum]
End Property

Private Sub Class_Initialize()
    'creates the collection when this class is created
    Set mCol = New Collection
End Sub

I am new to vb.net so I don't have any knowledge about working on these codes.Can someone please explain me its working and how can I code it in vb.net 我是vb.net的新手,所以我对使用这些代码一无所知。有人可以向我解释其工作方式以及如何在vb.net中对其进行编码

This is the vb6 function for collection 这是vb6的收集功能

Public Function Add(Key As String, Optional sKey As String) As clsUser_Rights
    'create a new object
    Dim objNewMember As clsUser_Rights
    Set objNewMember = New clsUser_Rights

    'set the properties passed into the method
    objNewMember.Key = Key
    If Len(sKey) = 0 Then
        mCol.Add objNewMember
    Else
        mCol.Add objNewMember, sKey
    End If

    'return the object created
    Set Add = objNewMember
    Set objNewMember = Nothing
End Function

and this is what I tried 这就是我尝试过的

Private mCol As New Dictionary(Of string,string)

Public Function Add(Key As String, Optional sKey As String = "") As clsMsmt
    'create a new object
    Dim objNewMember As clsMsmt
    objNewMember = New clsMsmt

    'set the properties passed into the method
    objNewMember.Key = Key
    If sKey.Length = 0 Then
        mCol.Add(objNewMember)
    Else
        mCol.Add(objNewMember, sKey)
    End If

    'return the object created
    Add = objNewMember
    objNewMember = Nothing
End Function

The VB6 code that you highlighted looks like a custom wrapper class for the built-in Collection type. 您突出显示的VB6代码看起来像是内置Collection类型的自定义包装器类。 The enumerator part is to allow For Each ... Next on the custom collection. 枚举器部分允许自定义集合上的For Each ... Next

Depending on the purpose of the collection class, you might not need it in .NET. 根据收集类的目的,.NET中可能不需要它。 One reason why custom collection classes were made in VB6 was to provide type safety, since Collection would only provide Object . 在VB6中创建自定义集合类的原因之一是提供类型安全性,因为Collection仅提供Object For this use, you can use either List (Of T) or Dictionary (Of TKey, TValue) depending on how the collection was used. 对于此用途,您可以使用List (Of T)Dictionary (Of TKey, TValue)具体取决于集合的使用方式。

If there is additional logic in the collection, you might still stick with the framework classes and add one or more extension methods to handle the additional logic, or you might inherit from Collection (Of T) or KeyedCollection (Of TKey, TItem) . 如果集合中包含其他逻辑,则您可能仍会坚持使用框架类,并添加一个或多个扩展方法来处理其他逻辑,或者您可能继承自Collection (Of T)KeyedCollection (Of TKey, TItem) The base classes will provide the collection boilerplate logic, and you can focus on providing the additional logic in your inherited class. 基类将提供集合样板逻辑,您可以集中精力在继承的类中提供其他逻辑。

If the code that used the VB6 collection was indexed by both string and integer, then you might need to do a little more legwork to get a working .NET equivalent, but I wouldn't tend to expect this (and even if it was done, the most likely use case might have been for removing items, and you could rewrite that to work correctly with a string-indexed .NET dictionary). 如果使用字符串和整数同时索引了使用VB6集合的代码,那么您可能需要做更多的工作才能获得等效的.NET,但是我不会期望这样做(即使这样做也是如此) ,最可能的用例可能是删除项目,您可以重写该代码以使其与字符串索引的.NET词典一起正常工作)。

did you try google your question first? 您是否首先尝试使用Google搜索您的问题? I think you did not. 我想你没有。 But anyway, here is a hint: 但是无论如何,这是一个提示:

' wrong: Dim mCcol As New Microsoft.VisualBasic.Collection()  

' correct: 
Dim mCcol As New Collection()  

sorry, tried it in C# first, and in VB.NET this assembly is referenced by default. 抱歉,首先在C#中进行了尝试,并且在VB.NET中默认引用了该程序集。

Added new sample (in empty WinForm:) 添加了新示例(在空WinForm中:)

    Dim dict As New Dictionary(Of String, String)
    dict.Add("KEY1", "dict: Some kind of stringdata")
    dict.Add("KEY2", "dict: other string data")
    dict.Add("KEY3", "dict: and finally: a string")

    For Each s As KeyValuePair(Of String, String) In dict
        MessageBox.Show(s.Value)
    Next

Replace the second String in the definition with your type (clsMsmt) 用您的类型(clsMsmt)替换定义中的第二个String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM