Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Creating a Custom Enumerator in a Base Class

Reply
 
Old 09-19-2008   #1 (permalink)
Charles Law


 
 

Creating a Custom Enumerator in a Base Class

I have a base class MyBaseClass, and several classes that inherit from it:
MyClass1, MyClass2, etc.

The base class implements IEnumerable(Of IMyBaseClassRow). The base class
has a DataTable object that contains different data depending on which of
MyClass1, MyClass2 are instantiated.

I want to be able to iterate through the rows of the data table using For
Each on my derived classes retrieving a custom row with properties specific
to that child class. I found this article

http://blog.falafel.com/2007/01/30/C...Iterators.aspx

which alludes to the technique, but doesn't go all the way to showing how to
derive custom row classes with the properties. I am also doing this in VB,
so I don't have Yield Return. Therefore, I am having to code this all by
hand.

I want all the enumeration stuff in the base class, so that I don't have to
reproduce it in every derived class, but I have one stumbling block. This is
my implementation of Current() in my base class:

Private ReadOnly Property Current() As IMyBaseClassRow Implements
System.Collections.Generic.IEnumerator(Of IMyBaseClassRow).Current
Get
If m_CurrentRow = -1 Or m_CurrentRow >
m_MyBaseClass.DataTable.Rows.Count - 1 Then
Throw New InvalidOperationException
End If

Return New
MyBaseClassRow(m_MyBaseClass.DataTable.Rows(m_CurrentRow))
End Get
End Property

The problem is that this can only return a new MyBaseClassRow, and not a
MyClass1Row. Therefore, when I do

For Each row As MyClass1Row in MyClass1Instance
...
Next

the compiler is happy, but it fails at runtime with an InvalidCastException
on the For Each line.

I think I need to override something in my MyClass1 or MyClass1Row class,
but I'm not sure what exactly, or how.

If this problem makes sense to anyone, please feel free to chip in. Any and
all help welcome.

TIA

Charles



My System SpecsSystem Spec
Old 09-19-2008   #2 (permalink)
Charles Law


 
 

Re: Creating a Custom Enumerator in a Base Class

I think I've cracked it.

In my base class MyBaseClass, I have created a must override function that
returns an object of the type I want, i.e.

Protected MustOverride Function GetIMyBaseClassRow(dr As DataRow) As
IMyBaseClassRow

In my concrete class, I then override this function:

Protected Overrides Function GetIMyBaseClassRow(ByVal dr As
System.Data.DataRow) As IMyBaseClassRow
Return New MyClass1Row(dr)
End Function

Finally, Current() in MyBaseClass becomes this:

Private ReadOnly Property Current() As IMyBaseClassRow Implements
System.Collections.Generic.IEnumerator(Of IMyBaseClassRow).Current
Get
If m_CurrentRow = -1 Or m_CurrentRow >
m_MyBaseClass.DataTable.Rows.Count - 1 Then
Throw New InvalidOperationException
End If

Return
GetIMyBaseClassRow(m_MyBaseClass.DataTable.Rows(m_CurrentRow))
End Get
End Property

It allows the base class to call the derived class to get an object of the
actual type required.

I hope this helps someone.

If anyone has another solution, I am still very interested to hear.

Charles


"Charles Law" <blank@xxxxxx> wrote in message
news:OWpQYelGJHA.4408@xxxxxx
Quote:

>I have a base class MyBaseClass, and several classes that inherit from it:
>MyClass1, MyClass2, etc.
>
> The base class implements IEnumerable(Of IMyBaseClassRow). The base class
> has a DataTable object that contains different data depending on which of
> MyClass1, MyClass2 are instantiated.
>
> I want to be able to iterate through the rows of the data table using For
> Each on my derived classes retrieving a custom row with properties
> specific to that child class. I found this article
>
> http://blog.falafel.com/2007/01/30/C...Iterators.aspx
>
> which alludes to the technique, but doesn't go all the way to showing how
> to derive custom row classes with the properties. I am also doing this in
> VB, so I don't have Yield Return. Therefore, I am having to code this all
> by hand.
>
> I want all the enumeration stuff in the base class, so that I don't have
> to reproduce it in every derived class, but I have one stumbling block.
> This is my implementation of Current() in my base class:
>
> Private ReadOnly Property Current() As IMyBaseClassRow Implements
> System.Collections.Generic.IEnumerator(Of IMyBaseClassRow).Current
> Get
> If m_CurrentRow = -1 Or m_CurrentRow >
> m_MyBaseClass.DataTable.Rows.Count - 1 Then
> Throw New InvalidOperationException
> End If
>
> Return New
> MyBaseClassRow(m_MyBaseClass.DataTable.Rows(m_CurrentRow))
> End Get
> End Property
>
> The problem is that this can only return a new MyBaseClassRow, and not a
> MyClass1Row. Therefore, when I do
>
> For Each row As MyClass1Row in MyClass1Instance
> ...
> Next
>
> the compiler is happy, but it fails at runtime with an
> InvalidCastException on the For Each line.
>
> I think I need to override something in my MyClass1 or MyClass1Row class,
> but I'm not sure what exactly, or how.
>
> If this problem makes sense to anyone, please feel free to chip in. Any
> and all help welcome.
>
> TIA
>
> Charles
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Creating a class in a HTA VB Script
Base class defined and initialized variables loose initialization in subclass. .NET General
Using a Delegate to Simulate Invoking Events in Base Class .NET General
Creating a WMI class PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46