Collection Classes

Collection Classes

Upgrading collections built with the Visual Basic Class Builder utility is fairly straightforward. In the typical case, you will need to uncomment only a single line of code to get the collection to work in Visual Basic .NET. Of course, the ease of the upgrade depends a great deal on what modifications were made to the collection and how closely it resembles the originally generated collection class. In most cases, the Upgrade Wizard will insert a simple ToDo comment that requires a minor modification to get your collection to work as expected. The following example is a simple string collection class implemented in Visual Basic 6 using the Class Wizard Utility:

'Local variable to hold collection Private mCol As Collection Public Function Add(Key As String, Value As String) As String    'Set the properties passed into the method    mCol.Add Value, Key    Add = Value End Function Public Property Get Item(vntIndexKey As Variant) As String    'Used when referencing an element in the collection    'vntIndexKey contains either the Index or Key to the collection,    'which is why it is declared as a Variant    'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)    Set Item = mCol(vntIndexKey) End Property Public Property Get Count() As Long    'Used when retrieving the number of elements in the    'collection. Syntax: Debug.Print x.Count    Count = mCol.Count End Property Public Sub Remove(vntIndexKey As Variant)    'Used when removing an element from the collection    'vntIndexKey contains either the Index or Key, which is why    'it is declared as a Variant    'Syntax: x.Remove(xyz)    mCol.Remove vntIndexKey End Sub 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 Private Sub Class_Terminate()    'Destroys collection when this class is terminated    Set mCol = Nothing End Sub

Essentially, what will happen when you upgrade this code is that the NewEnum property on the collection will be commented out and replaced with a GetEnumerator method. The following example is the complete Visual Basic .NET version of the previous collection class as created by the Upgrade Wizard. Notice both the NewEnum property and the GetEnumerator method.

Friend Class StringCollection    Implements System.Collections.IEnumerable    'Local variable to hold collection    Private mCol As Collection    Public Function Add(ByRef Key As String, _                        ByRef Value As String) As String       'Set the properties passed into the method       mCol.Add(Value, Key)       Add = Value    End Function    Default Public ReadOnly Property Item(ByVal vntIndexKey As Object) _       As String       Get          'Used when referencing an element in the collection          'vntIndexKey contains either the Index or Key to the           'collection,          'which is why it is declared as a Variant          'Syntax: Set foo = x.Item(xyz) or Set foo = x.Item(5)          Item = mCol.Item(vntIndexKey)       End Get    End Property    Public ReadOnly Property Count() As Integer       Get          'Used when retrieving the number of elements in the          'collection. Syntax: Debug.Print x.Count          Count = mCol.Count()       End Get    End Property    'UPGRADE_NOTE: NewEnum property was commented out.    'Public ReadOnly Property NewEnum() As stdole.IUnknown       'Get          'This property allows you to enumerate          'this collection with the For...Each syntax          'NewEnum = mCol._NewEnum       'End Get    'End Property    Public Function GetEnumerator() As System.Collections.IEnumerator _       Implements System.Collections.IEnumerable.GetEnumerator       'UPGRADE_TODO: Uncomment and change the following line to return        'the collection enumerator.       'GetEnumerator = mCol.GetEnumerator    End Function    Public Sub Remove(ByRef vntIndexKey As Object)       'Used when removing an element from the collection       'vntIndexKey contains either the Index or Key, which is why       'it is declared as a Variant       'Syntax: x.Remove(xyz)       mCol.Remove(vntIndexKey)    End Sub    'UPGRADE_NOTE: Class_Initialize was upgraded to     'Class_Initialize_Renamed    Private Sub Class_Initialize_Renamed()       'Creates the collection when this class is created       mCol = New Collection    End Sub    Public Sub New()       MyBase.New()       Class_Initialize_Renamed()    End Sub    'UPGRADE_NOTE: Class_Terminate was upgraded to     'Class_Terminate_Renamed.    Private Sub Class_Terminate_Renamed()       'Destroys collection when this class is terminated       'UPGRADE_NOTE: Object mCol may not be destroyed until it is        'garbage collected.       mCol = Nothing    End Sub    Protected Overrides Sub Finalize()       Class_Terminate_Renamed()       MyBase.Finalize()    End Sub End Class

To enable your collection class, you need to uncomment a single line of code in the GetEnumerator method (the Visual Basic .NET equivalent of the NewEnum property) and make any necessary additional modifications. The vast majority of cases will not require additional modifications, but the Upgrade Wizard does not take any chances. By requiring you to uncomment the line in the GetEnumerator method, it forces you to evaluate whether you do in fact need to make any additional changes. Otherwise, just returning the Enumerator from the underlying collection class will be perfectly sufficient. To see this sample in action, check out the Visual Basic .NET version of the CollectionSample project on the companion CD.



Upgrading Microsoft Visual Basic 6.0to Microsoft Visual Basic  .NET
Upgrading Microsoft Visual Basic 6.0 to Microsoft Visual Basic .NET w/accompanying CD-ROM
ISBN: 073561587X
EAN: 2147483647
Year: 2001
Pages: 179

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net