T
tshad
A couple of questions on interfaces.
I am looking at interfaces and at an example program I found.
To set up a custom collection you must have IEnumerable and IEnumerator
implemented.
My code is:
**********************************************************************
using System;
using System.Collections;
namespace CustomCollection
{
internal class MyClass
{
public string Name;
public int Age;
//Default constructor
public MyClass()
{
}
//This parametrised constructor will assign value to this class data
members.
public MyClass(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
public class Iterator : IEnumerator, IEnumerable
{
// Instantiating a collection of MyClass type.
private MyClass[] ClassArray;
int Cnt;
public Iterator()
{
//Assigning values in default constructor
ClassArray = new MyClass[4];
ClassArray[0] = new MyClass("Kith", 23);
ClassArray[1] = new MyClass("Smith", 30);
ClassArray[2] = new MyClass("Geo", 19);
ClassArray[3] = new MyClass("Greg", 14);
}
//Defanision for IEnumerator.Reset() method.
public void Reset()
{
Cnt = -1;
}
//Defanision for IEnumerator.MoveNext() method.
public bool MoveNext()
{
return (++Cnt < ClassArray.Length);
}
//Defanision for IEnumerator.Current Property.
public object Current
{
get
{
return ClassArray[Cnt];
}
}
//Defanision for IEnumerable.GetEnumerator() method.
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
static void Main()
{
IEnumerator temp;
//Now this instance will contain a collection of MyClass Tyes
Iterator It = new Iterator();
//Am trying to access like normal array
foreach (MyClass MY in It)
{
temp = It.GetEnumerator();
Console.WriteLine("Name : " + MY.Name.ToString());
Console.WriteLine("Age : " + MY.Age.ToString());
}
Console.Read();
}
}
}
**********************************************************************
I understand how it works but I am curious why you need the ": IEnumerable"
on my Iterator class.
If I change the line to the following:
public class Iterator : IEnumerator
It works fine without it since I have all the methods implemented.
So what is the point in having it there?
The only reason I think I need IEnumerator there is because I am passing
back an object of type "IEnumerator" and if it wasn't there, Iterator
wouldn't be of type IEnumerator - even though the GetEnumerator() method is
implemented.
If I don't have it there, I get an error on the return statement of
GetEnumerator() saying that
Unable to cast object of type 'CustomCollection.Iterator' to type
'System.Collections.IEnumerator'.
I assume that if I weren't returning an IEnumerator - this wouldn't be
needed, either - just the method.
Thanks,
Tom
I am looking at interfaces and at an example program I found.
To set up a custom collection you must have IEnumerable and IEnumerator
implemented.
My code is:
**********************************************************************
using System;
using System.Collections;
namespace CustomCollection
{
internal class MyClass
{
public string Name;
public int Age;
//Default constructor
public MyClass()
{
}
//This parametrised constructor will assign value to this class data
members.
public MyClass(string name, int age)
{
this.Name = name;
this.Age = age;
}
}
public class Iterator : IEnumerator, IEnumerable
{
// Instantiating a collection of MyClass type.
private MyClass[] ClassArray;
int Cnt;
public Iterator()
{
//Assigning values in default constructor
ClassArray = new MyClass[4];
ClassArray[0] = new MyClass("Kith", 23);
ClassArray[1] = new MyClass("Smith", 30);
ClassArray[2] = new MyClass("Geo", 19);
ClassArray[3] = new MyClass("Greg", 14);
}
//Defanision for IEnumerator.Reset() method.
public void Reset()
{
Cnt = -1;
}
//Defanision for IEnumerator.MoveNext() method.
public bool MoveNext()
{
return (++Cnt < ClassArray.Length);
}
//Defanision for IEnumerator.Current Property.
public object Current
{
get
{
return ClassArray[Cnt];
}
}
//Defanision for IEnumerable.GetEnumerator() method.
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
static void Main()
{
IEnumerator temp;
//Now this instance will contain a collection of MyClass Tyes
Iterator It = new Iterator();
//Am trying to access like normal array
foreach (MyClass MY in It)
{
temp = It.GetEnumerator();
Console.WriteLine("Name : " + MY.Name.ToString());
Console.WriteLine("Age : " + MY.Age.ToString());
}
Console.Read();
}
}
}
**********************************************************************
I understand how it works but I am curious why you need the ": IEnumerable"
on my Iterator class.
If I change the line to the following:
public class Iterator : IEnumerator
It works fine without it since I have all the methods implemented.
So what is the point in having it there?
The only reason I think I need IEnumerator there is because I am passing
back an object of type "IEnumerator" and if it wasn't there, Iterator
wouldn't be of type IEnumerator - even though the GetEnumerator() method is
implemented.
If I don't have it there, I get an error on the return statement of
GetEnumerator() saying that
Unable to cast object of type 'CustomCollection.Iterator' to type
'System.Collections.IEnumerator'.
I assume that if I weren't returning an IEnumerator - this wouldn't be
needed, either - just the method.
Thanks,
Tom