Witam,
mam problem z implementacją interfejsów IEnumerable i IEnumerator. Kolekcją w mojej klasie jest tablica 2d. Za każdym razem, gdy próbuję zastosować foreach, program sypie wyjątkiem InvalidCastexception. Problem leży gdzieś w metodzie Current.
#region IEnumerator i IEnumerable
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
try
{
if (pos.Q < tab.GetLength(1)) pos.Q++;
else
{
pos.P++;
pos.Q = 0;
}
}
catch(Exception ex)
{
Console.WriteLine("Exception in MoveNext():" +ex.Message);
//Reset();
return false;
}
return true;
}
public void Reset()
{
pos.P = 0;
pos.Q =0;
}
public object Current
{
get
{
try
{
return tab[pos.P, pos.Q];
}
catch ( Exception ex)
{
Console.WriteLine("Exception in current: "+ex);
return ex;
}
}
}
#endregion