473,569 Members | 2,458 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.Collecti ons.Generic.IEn umerator(Of IMyBaseClassRow ).Current
Get
If m_CurrentRow = -1 Or m_CurrentRow >
m_MyBaseClass.D ataTable.Rows.C ount - 1 Then
Throw New InvalidOperatio nException
End If

Return New
MyBaseClassRow( m_MyBaseClass.D ataTable.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 MyClass1Instanc e
...
Next

the compiler is happy, but it fails at runtime with an InvalidCastExce ption
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 1970
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 GetIMyBaseClass Row(dr As DataRow) As
IMyBaseClassRow

In my concrete class, I then override this function:

Protected Overrides Function GetIMyBaseClass Row(ByVal dr As
System.Data.Dat aRow) As IMyBaseClassRow
Return New MyClass1Row(dr)
End Function

Finally, Current() in MyBaseClass becomes this:

Private ReadOnly Property Current() As IMyBaseClassRow Implements
System.Collecti ons.Generic.IEn umerator(Of IMyBaseClassRow ).Current
Get
If m_CurrentRow = -1 Or m_CurrentRow >
m_MyBaseClass.D ataTable.Rows.C ount - 1 Then
Throw New InvalidOperatio nException
End If

Return
GetIMyBaseClass Row(m_MyBaseCla ss.DataTable.Ro ws(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******** ******@TK2MSFTN GP04.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.Collecti ons.Generic.IEn umerator(Of IMyBaseClassRow ).Current
Get
If m_CurrentRow = -1 Or m_CurrentRow >
m_MyBaseClass.D ataTable.Rows.C ount - 1 Then
Throw New InvalidOperatio nException
End If

Return New
MyBaseClassRow( m_MyBaseClass.D ataTable.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 MyClass1Instanc e
...
Next

the compiler is happy, but it fails at runtime with an
InvalidCastExce ption 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
1787
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 straightforward and dare I say intuitive? (well, as intuitive as you can get w/ MFC.) Anyhow, I am wanting to create some controls of my own w/ C#. ...
15
6729
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 javascript or vbscript it works. I will appreciate any help. I do the following (C#):
0
1813
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 that simply by using custom actions . 1st create setup and deployment project... add->new project->windows application name it ReadmeDilogue now...
3
1868
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
1932
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
18096
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 ListView columns so it should be doable, thanks
26
5343
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... ...
1
1635
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 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...
0
1830
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 quote based on a random index. my web.config section looks like this: <configSections> ... <section name="quotesSection" type="QuotesSection"/>...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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 we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.