Skip to content

Instantly share code, notes, and snippets.

@Nilzor
Created October 13, 2011 16:32
Show Gist options
  • Save Nilzor/1284715 to your computer and use it in GitHub Desktop.
Save Nilzor/1284715 to your computer and use it in GitHub Desktop.
Items-enumerator for NetOffice
/// <summary>
/// Returns an enumerator for the _Items class
/// </summary>
/// <returns>An enumerator for the _Items class</returns>
public IEnumerator GetEnumerator()
{
return new ItemsEnumerator(this);
}
#endregion
/// <summary>
/// Enumerator for _Items
/// </summary>
public class ItemsEnumerator : IEnumerator
{
private _Items _items;
private COMObject _current;
public ItemsEnumerator(_Items items)
{
_items = items;
}
#region IEnumerator Members
public object Current
{
get { return _current; }
}
public bool MoveNext()
{
try
{
COMObject item = null;
if (_current == null) item = _items.GetFirst(); // Unsure if this if is neccessary or if moveNext could be called as first call
else item = _items.GetNext();
if (item != null)
{
_current = item; // We don't want to reset _current if moveNext is attempted after the last element
return true;
}
}
catch (System.Exception) { }
return false;
}
public void Reset()
{
_current = null;
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment