المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Enumerate Custom Class



C# Programming
07-02-2009, 07:00 PM
Hi all

Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program.

I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

public class MyClass : IEnumerable
{
public System.DateTime datStart; //Schedule start date
public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
public int intParam1; //Various Uses
public int intParam2; //Various Uses
//Other fields remove for clarity

//Constructor
public MyClass()
{
//Set defaults for new MyClass()
datStart = new DateTime(1900, 1, 1);
datEnd = datStart;
intParam1 = -1;
intParam2 = -1;
}

IEnumerator IEnumerable.GetEnumerator()
{
//What goes here?

return null; //Only added this line to stop compiler error
}
}

I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem.

Any help would be MUCH appreciated.

Thank you.