Reputation: 27
I am converting vb6 project to vb.net but have stuck on these
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
This is the vb6 function for collection
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
Upvotes: 0
Views: 1712
Reputation: 2474
The VB6 code that you highlighted looks like a custom wrapper class for the built-in Collection
type. The enumerator part is to allow For Each ... Next
on the custom collection.
Depending on the purpose of the collection class, you might not need it in .NET. One reason why custom collection classes were made in VB6 was to provide type safety, since Collection
would only provide Object
. For this use, you can use either List (Of T)
or Dictionary (Of TKey, TValue)
depending on how the collection was used.
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)
. 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).
Upvotes: 1
Reputation: 2060
did you try google your question first? 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.
Added new sample (in empty 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)
Upvotes: 0