Iterating a DataView with Generics?

G

Guest

Hi there,

Before Generics I could iterate a DataView in this way:

using System.Collections;
private IEnumerator mIterator;

mIterator = mDataview.GetEnumerator();
mIterator.MoveNext();
DataRow mDatarow = ((DataRowView)mIterator.Current).Row;

Now with Generics I'm trying this:

using System.Collections.Generic;
private IEnumerator<DataRowView> mIterator;

But, in this line:
mIterator = mDataview.GetEnumerator();

I get this error:

Cannot implicitly convert type 'System.Collections.IEnumerator' to
'System.Collections.Generic.IEnumerator<System.Data.DataRowView>'. An
explicit conversion exists (are you missing a cast?)

What am I doing wrong? Is it the GetEnumerator() method that is not fully
compatible with the new Generics IEnumerator?

Thanks in advance,

-Benton
 
D

David Browne

Benton said:
Hi there,

Before Generics I could iterate a DataView in this way:

using System.Collections;
private IEnumerator mIterator;

mIterator = mDataview.GetEnumerator();
mIterator.MoveNext();
DataRow mDatarow = ((DataRowView)mIterator.Current).Row;

Now with Generics I'm trying this:

using System.Collections.Generic;
private IEnumerator<DataRowView> mIterator;

But, in this line:
mIterator = mDataview.GetEnumerator();

I get this error:

You should always have been iterating the DataView like this:

foreach (DataRowView r in mDataview)
{
//whatever
}

Why do you want to write extra code?
Cannot implicitly convert type 'System.Collections.IEnumerator' to
'System.Collections.Generic.IEnumerator<System.Data.DataRowView>'. An
explicit conversion exists (are you missing a cast?)

What am I doing wrong? Is it the GetEnumerator() method that is not fully
compatible with the new Generics IEnumerator?

DataRowView implements IEnumerable, NOT IEnumerable<DataRowView>. If it
were rewritten today, it would probably implement IEnumerable<DataRowView>.
But it just wasn't rewritten for .NET 2.0.

It's fully compatible with IEnumerator which is all that's really required.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top