473,508 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Curr entRow))
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
Sep 19 '08 #1
1 1628
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" <bl***@nowhere.comwrote in message
news:OW**************@TK2MSFTNGP04.phx.gbl...
>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_Curr entRow))
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


Sep 19 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1785
by: Jim | last post by:
I'm a little new to the C# way of doing thisgs... Although I've done this sort of thing before in VS 6 / MFC... creatnig controls, ActiveX controls, etc. Creating controls in MFC is pretty...
15
6719
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
3
1844
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that...
0
1809
by: shanthsp2002 | last post by:
well friends i have a small tip here which may be helpfull for u there may be situations where we need to use a customized dilogue box while doveloping setup and dyployment project, so u can do...
3
1864
by: hharry | last post by:
hello all, quick syntax question. i am writing a custom exception class and came across an example: public class dataProvException : ApplicationException { public dataProvException() { //
17
1918
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
11
18070
by: Pete Kane | last post by:
Hi All, does anyone know how to add TabPages of ones own classes at design time ? ideally when adding a new TabControl it would contain tab pages of my own classes, I know you can achieve this with...
26
5335
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
1
1966
by: Charles Law | last post by:
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...
0
1824
by: captainB | last post by:
greetings! In my asp.net application, I created a custom section in web.config that contain strings that are quotes to display in my home page. Every request for my home page (default.aspx) grabs a...
0
7125
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5631
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5055
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4709
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3186
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1561
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
767
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.