Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest that you start by creating functions GetEnumeratorInFirstStyle, GetEnumeratorInSecondStyle, etc. (use names suitable to your application, of course), and then create new structures something like (example in vb syntax, but should be easily convertible to C#):</p> <pre> Class enumTest Function GetEnumeratorInFirstStyle() As IEnumerator(Of Integer) Return Enumerable.Empty(Of Integer)() ' Real code would do something better End Function Private Structure FirstStyleEnumerable Implements IEnumerable(Of Integer) Private myEnumTest As enumTest Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of Integer) Implements System.Collections.Generic.IEnumerable(Of Integer).GetEnumerator Return myEnumTest.GetEnumeratorInFirstStyle End Function Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return myEnumTest.GetEnumeratorInFirstStyle End Function Sub New(ByVal newEnumTest As enumTest) myEnumTest = newEnumTest End Sub End Structure Public ReadOnly Property AsFirstStyleEnumerable As IEnumerable(Of Integer) Get Return New FirstStyleEnumerable(Me) End Get End Property End Class </pre> <p>Note that a structure is used rather than a class, since using a class would require creating a new heap object and add an extra level of indirection to its access; the real purpose of the structure is to allow it to implement a "different" IEnumerable&lt;T&gt; from the encapsulated object. BTW, it would be possible to use generics with marker classes to avoid having to manually define a new FirstStyleEnumerator structure for each variation on enumeration. I'm not sure whether that would be cleaner or more confusing, though.</p> <pre> Interface IQualifiedEnumerable(Of T, U) Function GetEnumerator() As IEnumerable(Of U) End Interface Structure QualifiedEnumerableWrapper(Of T, U) Implements IEnumerable(Of U) Private myEnumerable As IQualifiedEnumerable(Of T, U) Public Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of U) Implements System.Collections.Generic.IEnumerable(Of U).GetEnumerator Return myEnumerable.GetEnumerator End Function Public Function GetEnumerator1() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator Return myEnumerable.GetEnumerator End Function Sub New(ByVal newEnumerable As IQualifiedEnumerable(Of T, U)) myEnumerable = newEnumerable End Sub End Structure Class EnumTest2 Implements IQualifiedEnumerable(Of FirstEnumerationStyle, Integer) Implements IQualifiedEnumerable(Of SecondEnumerationStyle, Integer) Private Class FirstEnumerationStyle ' Marker classes for generics End Class Private Class SecondEnumerationStyle End Class Private Function GetFirstStyleEnumerator() As System.Collections.Generic.IEnumerable(Of Integer) Implements IQualifiedEnumerable(Of FirstEnumerationStyle, Integer).GetEnumerator Return Enumerable.Empty(Of Integer)() End Function Private Function GetSecondStyleEnumerator() As System.Collections.Generic.IEnumerable(Of Integer) Implements IQualifiedEnumerable(Of SecondEnumerationStyle, Integer).GetEnumerator Return Enumerable.Empty(Of Integer)() End Function Public ReadOnly Property AsFirstStyleEnumerable As IEnumerable(Of Integer) Get Return New QualifiedEnumerableWrapper(Of FirstEnumerationStyle, Integer) End Get End Property Public ReadOnly Property AsSecondStyleEnumerable As IEnumerable(Of Integer) Get Return New QualifiedEnumerableWrapper(Of SecondEnumerationStyle, Integer) End Get End Property End Class </pre> <p>Here, the definitions of the interface and the structure are entirely general-purpose; adding each additional method of enumeration to a class would require adding a function to return its enumerator, and a property to return a QualifiedEnumerableWrapper of the appropriate type.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload