Migrating to System.Collections.IEnumerator from VB6.Collection 
Author Message
 Migrating to System.Collections.IEnumerator from VB6.Collection

Hello:

I have been attempting to introduce the IEnumerator interface to replace all
the VB6.Collection objects in my migrated VB6 code.  Having previously been
a delphi programmer I am estatic about the .Net platform, however, i am
having some difficulty due to the lack of examples in the MS .NET help.
Something Borland was very good about - eventually...  Anyway, looking at
this site in C# has been a help
http://www.*-*-*.com/
description for an optimized implementation of the IEnumerator interface
which overrides the GetEnumerator Method has me stumped in VB syntax.  I am
including the code below which appears not to work.  Also, assuming i get
this code working I am not sure how to .Add an object to the Enumerator. Any
help or examples or links to working code would be appreciated.

Thanks,

Mark Staelens
CA - VortalLogic

Public Class VLGenProperties

Implements IEnumerable

Private objGenProperty As EnumGenProperty

'Public Function GetEnumerator() As EnumGenProperty

' GetEnumerator = New EnumGenProperty()

'End Function

Public Function GetEnumerator() As Collections.IEnumerator Implements
System.Collections.IEnumerable.GetEnumerator

GetEnumerator = New EnumGenProperty()

End Function

End Class

Public Class EnumGenProperty

Implements IEnumerator

Private Obj As VLGenProperty

Private Cursor As Integer

ReadOnly Property Current() As Object Implements IEnumerator.Current

Get

Current = Obj

End Get

End Property

Public Sub Reset() Implements System.Collections.IEnumerator.Reset

Cursor = -1

End Sub

Public Function MoveNext() As Boolean Implements
System.Collections.IEnumerator.MoveNext

If Cursor > 0 Then cursor = cursor + 1

End Function

Public Function Enumerator() As VLGenProperty

Enumerator = New VLGenProperty()

End Function

End Class

Public Class VLGenProperty

Public Enum enumVariableType

varInteger = 2

varLong = 3

varSingle = 4

varDouble = 5

varCurrency = 6

varDate = 7

varString = 8

varDecimal = 14

End Enum

Private mstrVariableName As String

Private menumVariableType As enumVariableType

Public Property VariableName() As String

Get

Return mstrVariableName

End Get

Set

mstrVariableName = VariableName

End Set

End Property

Public Property VariableType() As enumVariableType

Get

Return menumVariableType

End Get

Set

menumVariableType = VariableType

End Set

End Property

End Class



Mon, 14 Jul 2003 05:47:01 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection

Quote:
> Hello:

> I have been attempting to introduce the IEnumerator interface to replace all
> the VB6.Collection objects in my migrated VB6 code.  Having previously been
> a Delphi programmer I am estatic about the .Net platform, however, i am
> having some difficulty due to the lack of examples in the MS .NET help.
> Something Borland was very good about - eventually...  Anyway, looking at
> this site in C# has been a help
> http://codeguru.earthweb.com/nothingButNet/enumeration.shtml but the authors
> description for an optimized implementation of the IEnumerator interface
> which overrides the GetEnumerator Method has me stumped in VB syntax.  I am
> including the code below which appears not to work.  Also, assuming i get
> this code working I am not sure how to .Add an object to the Enumerator. Any
> help or examples or links to working code would be appreciated.

A good place to start for a replacement to VB6 collections is the
System.Collections namespace.  If you want to go futher and create your
own collection class, see the System.Collections.Bases namespace.

Here's a "quick and dirty" example of a strongly-typed custom collection
class:

Public Class Employee
        Public FirstName As String
        Public LastName As String
        Public Age As Short

        Public Sub New(ByVal sFirst As String, ByVal sLast As String,
                                ByVal iAge As Short)
                FirstName = sFirst
                LastName = sLast
                Age = iAge
        End Sub

End Class

Public Class WorkForce
        Inherits System.Collections.Bases.TypedCollectionBase

        Public Function Add(ByVal oEmp As Employee) As Integer
                Return MyBase.InnerList.Add(oEmp)
        End Function

        Public Sub Remove(ByVal oEmp As Employee)
                MyBase.InnerList.Remove(oEmp)
        End Sub

        Public Function Item(ByVal iIdx As Integer) As Employee
                Return CType(MyBase.InnerList.Item(iidx), Employee)
        End Function

End Class

--
Patrick Steele

Lead Software Architect



Mon, 14 Jul 2003 06:03:57 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection
Patrick - Thanks for the quick response and very clean sample.

The sample code at CodeGuru (see below) in C# was optimized to not cast the
return object.  Supposedly it is much faster to return the object inside an
implementation of the Enumerator.  I wonder if using the
System.Collections.Bases.TypedCollectionBase Class is optimized for
performance.  If anyone has any comments i would appreciate it, otherwise i
will be using the TypedCollectionBase as posted.

Thank again

Mark Staelens
CA - VortalLogic


Quote:

> > Hello:

> > I have been attempting to introduce the IEnumerator interface to replace
all
> > the VB6.Collection objects in my migrated VB6 code.  Having previously
been
> > a Delphi programmer I am estatic about the .Net platform, however, i am
> > having some difficulty due to the lack of examples in the MS .NET help.
> > Something Borland was very good about - eventually...  Anyway, looking
at
> > this site in C# has been a help
> > http://codeguru.earthweb.com/nothingButNet/enumeration.shtml but the
authors
> > description for an optimized implementation of the IEnumerator interface
> > which overrides the GetEnumerator Method has me stumped in VB syntax.  I
am
> > including the code below which appears not to work.  Also, assuming i
get
> > this code working I am not sure how to .Add an object to the Enumerator.
Any
> > help or examples or links to working code would be appreciated.

> A good place to start for a replacement to VB6 collections is the
> System.Collections namespace.  If you want to go futher and create your
> own collection class, see the System.Collections.Bases namespace.

> Here's a "quick and dirty" example of a strongly-typed custom collection
> class:

> Public Class Employee
> Public FirstName As String
> Public LastName As String
> Public Age As Short

> Public Sub New(ByVal sFirst As String, ByVal sLast As String,
> ByVal iAge As Short)
> FirstName = sFirst
> LastName = sLast
> Age = iAge
> End Sub

> End Class

> Public Class WorkForce
> Inherits System.Collections.Bases.TypedCollectionBase

> Public Function Add(ByVal oEmp As Employee) As Integer
> Return MyBase.InnerList.Add(oEmp)
> End Function

> Public Sub Remove(ByVal oEmp As Employee)
> MyBase.InnerList.Remove(oEmp)
> End Sub

> Public Function Item(ByVal iIdx As Integer) As Employee
> Return CType(MyBase.InnerList.Item(iidx), Employee)
> End Function

> End Class

> --
> Patrick Steele

> Lead Software Architect



Mon, 14 Jul 2003 06:39:16 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection
Mark,

Quote:
> The sample code at CodeGuru (see below) in C# was optimized to not cast
the
> return object.  Supposedly it is much faster to return the object inside
an
> implementation of the Enumerator.  I wonder if using the
> System.Collections.Bases.TypedCollectionBase Class is optimized for
> performance.  If anyone has any comments i would appreciate it, otherwise
i
> will be using the TypedCollectionBase as posted.

The optimisation technique in the CodeGuru sample is only really practical
if you are creating a collection of value-types (primitives or structures),
since it avoids boxing items into objects when stored, and unboxing them
when accessed.  In this case the DIY enumerator can avoid that, and
store/return value-types without boxing.

If you're implementing a collection of reference-types (real objects), then
I imagine the performance gain will be neglible since casting is not that
expensive.  Patrick's sample is the more practical design in this case, and
still provides type safety.

--
David.



Mon, 14 Jul 2003 09:17:14 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection
Hi Patrick,

I noticed in both your "Item" and "Add" methods that there does not appear
to be support for either adding an item to a collection using a key nor
retrieving an item from a collection using a key.  Is there no workaround
for this?

Thanks.

--
Mitch Abaza



Mon, 14 Jul 2003 12:16:14 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection

Quote:
> Hi Patrick,

> I noticed in both your "Item" and "Add" methods that there does not appear
> to be support for either adding an item to a collection using a key nor
> retrieving an item from a collection using a key.  Is there no workaround
> for this?

You would need to inherit from "TypedDictionaryBase" instead of
"TypedCollectionBase" and then make the appropriate changes -- i.e. the
Add method could accept a key and pass it to
"MyBase.Dictionary.Add(key,value)".  The Item method would also need
changing to access the MyBase.Dictionary object accordingly.

--
Patrick Steele

Lead Software Architect



Tue, 15 Jul 2003 00:11:32 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection
Thanks much for the response Patrick.  Any speed advantages to use the
TypedCollectionBase vs TypedDyctionaryBase?


Tue, 15 Jul 2003 08:25:12 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection

Quote:
>I noticed in both your "Item" and "Add" methods that there does not appear
>to be support for either adding an item to a collection using a key nor
>retrieving an item from a collection using a key.  Is there no workaround
>for this?

http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemcollections...
http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemcollections...
http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemcollections...
http://msdn.microsoft.com/library/dotnet/cpref/frlrfsystemcollections...


Tue, 15 Jul 2003 08:31:59 GMT  
 Migrating to System.Collections.IEnumerator from VB6.Collection

Quote:
> Thanks much for the response Patrick.  Any speed advantages to use the
> TypedCollectionBase vs TypedDyctionaryBase?

None that I know of.  However, beta 1 is not the version you want to do
any timing/performance testing with.  :)

--
Patrick Steele

Lead Software Architect



Tue, 15 Jul 2003 11:12:26 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Collections, Collections and More Collections

2. Document collections and AllForms collections

3. Creating a CDO collection from an Outlook collection

4. using collection of collections

5. Storing a collection in a collection

6. collection in collection

7. Persisting a Collection of a Collection

8. Collection of Collections?

9. Collections, swapping items in a collection

10. Collections of Collections..how?

11. collections of collections

12. Collection of Collections

 

 
Powered by phpBB® Forum Software