Custom Class and IEnumerable

  • Thread starter Thread starter Troy
  • Start date Start date
T

Troy

Hi, I'm trying to use my custom class as a data provider
for a datagrid datasource. Through all the documentation
I can find, apparently my class must implement the
IEnumerable interface. I am rather perplexed as where to
go from here though. I believe I need to now use the
Function GetEnumerator() within my class but don't know
how.

Any guidance would be greatly appreciated in pursuing this
futher. Thank you.

Troy
 
Troy,
This is a bit complicated to show in a newsgroup post
but I'll try.

You are correct in that your class must Implement the
IEnumerable interface. That interface as 1 method you must
create and that is GetEnumerator. GetEnumerator must
return an instance of an object that implements
IEnumerator.

IEnumerator has the following methods/properties you must
implement:

bool MoveNext();
object Current{get;set;}
void Reset();

Technically your data provider class could implement both
interfaces. As long as you have the correct code in the
IEnumerator methods it should work.

the example below is really vague, but I hope it helps.
I'm assuming that your using a DataView as your source.
Just for example purposes let's have your data provider
return a first and last name property.

If you follow the code below, I made the data provider
implement both interfaces. The Current property
implementation returns an instance of a custom structure
with a FirstName and LastName property. You should bind to
those Property names.

The code below was not created in VS.Net. I'm not even
sure it compiles. Hopefully this will lead you in the
right direction. I would use the code as a base for
learning how these interfaces work. Once you understand
them I would try and setup some sort of a framework of
base classes that will allow you to do this easily in all
of your custom data providers.


public struct MyDataStruct
{
private _firstName;
private _lastName;

public MyDataStruct(string firstName,string lastName)
{
this._firstName = firstName;
this._lastName = lastName;
}

public string FirstName
{
get{return(this._firstName);}
}

public string LastName
{
get{return(this._lastName);}
}
}

public class myDataProvider : IEnumerable, IEnumerator
{
private int _index = -1;
private DataView _myView = null;

IEnumerator IEnumerable.GetEnumerator()
{
return(this);
}

void IEnumerator.Reset()
{
this._index = -1;
}

obect IEnumerator.Current
{
set{}
get
{
MyDataStruct rtnData = new MyDataStruct(this._myView
[this._index].Row["FIRST_NAME"].ToString(),this._myView
[this._index].Row["LAST_NAME"].ToString());



return(rtnData);
}

bool IEnumerator.MoveNext()
{
this._index++;
try
{
return(this._index < this._myView.Count);
}
catch
{
return(false);
}
}



}
 
Hi, I'm trying to use my custom class as a data provider
for a datagrid datasource. Through all the documentation
I can find, apparently my class must implement the
IEnumerable interface. I am rather perplexed as where to
go from here though. I believe I need to now use the
Function GetEnumerator() within my class but don't know
how.

Any guidance would be greatly appreciated in pursuing this
futher. Thank you.

Troy

The easiest way is for your class to inherit from CollectionBase. It
implements IEnumerable, IList, and ICollection. Want a sample? I have an
open source code generator that will write the collection for you...

Don't forget to download the appropriate template as well.

project main site:
http://sourceforge.net/projects/colcodegen

files:
http://sourceforge.net/project/showfiles.php?group_id=89362

file release notes/descriptions:
http://sourceforge.net/project/shownotes.php?release_id=182337

Michael Lang, MCSD
 
Back
Top