how to iterate SimpleDictionary that implements IDictionary?

R

Rich P

I am practicing creating a custom SimpleDictionary collection object
that implements IDictionary<int, MyClass> as follows below. I generated
the default members to this custom collection object from the context
menu of right-clicking on IDictionary<int, MyClass>. So far, I can add
elements to my custom collection object and I can retrieve a count of
elements, but I can't interate my collection object. I don't know what
to return here:

public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator()
{
return -- what do I return here?
}

and here is the code I have. Note: I have not implemented most of the
members. These have the default throw exception... How can I iterate
this custom collection object?

------------------------------------------------------
static void Main(string[] args)
{
MyClass e1, e2;
e1 = new MyClass(1001, "Andy Reid");
e2 = new MyClass(1002, "Kara Lang");

MyClassCollection eData = new MyClassCollection(2);
eData.Add(e1.EmpID, e1);
eData.Add(e2.EmpID, e1);
Console.WriteLine(eData.Count.ToString());

foreach (KeyValuePair<int, MyClass> de in eData)
{
Console.WriteLine(de.Key.ToString() + " " + de.Value.ToString());
}
}


public class MyClass
{
public int EmpID;
public string EmpName;

public MyClass(int i, string s)
{
EmpID = i;
EmpName = s;
}
}


public class MyClassCollection : IDictionary <int, MyClass>
{
// The array of items
private DictionaryEntry[] items;
private Int32 ItemsInUse = 0;
private Int32 Idx = 0;

//// Construct the SimpleDictionary with the desired number of items.
//// The number of items cannot change for the life time of this
SimpleDictionary.
public MyClassCollection(Int32 numItems)
{
items = new DictionaryEntry[numItems];
}

#region IDictionary<int,MyClass> Members

public void Add(int key, MyClass value)
{
if (ItemsInUse == items.Length)
throw new InvalidOperationException("The dictionary cannot hold
any more items.");
items[ItemsInUse++] = new DictionaryEntry(key, value);
}

public bool ContainsKey(int key)
{
throw new NotImplementedException();
}

public ICollection<int> Keys
{
get { throw new NotImplementedException(); }
}

public bool Remove(int key)
{
throw new NotImplementedException();
}

public bool TryGetValue(int key, out MyClass value)
{
throw new NotImplementedException();
}

public ICollection<MyClass> Values
{
get { throw new NotImplementedException(); }
}

public MyClass this[int key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

#endregion

#region ICollection<KeyValuePair<int,MyClass>> Members

public void Add(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

public void Clear()
{
throw new NotImplementedException();
}

public bool Contains(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

public void CopyTo(KeyValuePair<int, MyClass>[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public int Count
{
get { return ItemsInUse; }
}

public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}

public bool Remove(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

#endregion

#region IEnumerable<KeyValuePair<int,MyClass>> Members

public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator()
{
return -- what do I return here?
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}

#endregion
}


Thanks

Rich
 
R

Rick Lones

Rich said:
I am practicing creating a custom SimpleDictionary collection object
that implements IDictionary<int, MyClass> as follows below. I generated
the default members to this custom collection object from the context
menu of right-clicking on IDictionary<int, MyClass>. So far, I can add
elements to my custom collection object and I can retrieve a count of
elements, but I can't interate my collection object. I don't know what
to return here:

public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator()
{
return -- what do I return here?
}

return new MyClassCustomEnumerator<int, MyClass>(this);

where

public MyClassCustomEnumerator<int, MyClass> : IEnumnerator,
IEnumerator<int MyClass>

{
// implementation of constructor, IEnmerator members, etc. here
}

This class should be able to enumerate an instance of your custom dictionary.
What that means depends on how you have implemented your custom IEnumerable
collection. Probably you want to initialize it during construction with a
reference to the instance it will enumerate. Probably, since in this case your
dictionary is backed by a simple array, you only need to maintain a current
array index as state between MoveNext invocations.

HTH,
-rick-
and here is the code I have. Note: I have not implemented most of the
members. These have the default throw exception... How can I iterate
this custom collection object?

------------------------------------------------------
static void Main(string[] args)
{
MyClass e1, e2;
e1 = new MyClass(1001, "Andy Reid");
e2 = new MyClass(1002, "Kara Lang");

MyClassCollection eData = new MyClassCollection(2);
eData.Add(e1.EmpID, e1);
eData.Add(e2.EmpID, e1);
Console.WriteLine(eData.Count.ToString());

foreach (KeyValuePair<int, MyClass> de in eData)
{
Console.WriteLine(de.Key.ToString() + " " + de.Value.ToString());
}
}


public class MyClass
{
public int EmpID;
public string EmpName;

public MyClass(int i, string s)
{
EmpID = i;
EmpName = s;
}
}


public class MyClassCollection : IDictionary <int, MyClass>
{
// The array of items
private DictionaryEntry[] items;
private Int32 ItemsInUse = 0;
private Int32 Idx = 0;

//// Construct the SimpleDictionary with the desired number of items.
//// The number of items cannot change for the life time of this
SimpleDictionary.
public MyClassCollection(Int32 numItems)
{
items = new DictionaryEntry[numItems];
}

#region IDictionary<int,MyClass> Members

public void Add(int key, MyClass value)
{
if (ItemsInUse == items.Length)
throw new InvalidOperationException("The dictionary cannot hold
any more items.");
items[ItemsInUse++] = new DictionaryEntry(key, value);
}

public bool ContainsKey(int key)
{
throw new NotImplementedException();
}

public ICollection<int> Keys
{
get { throw new NotImplementedException(); }
}

public bool Remove(int key)
{
throw new NotImplementedException();
}

public bool TryGetValue(int key, out MyClass value)
{
throw new NotImplementedException();
}

public ICollection<MyClass> Values
{
get { throw new NotImplementedException(); }
}

public MyClass this[int key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}

#endregion

#region ICollection<KeyValuePair<int,MyClass>> Members

public void Add(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

public void Clear()
{
throw new NotImplementedException();
}

public bool Contains(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

public void CopyTo(KeyValuePair<int, MyClass>[] array, int arrayIndex)
{
throw new NotImplementedException();
}

public int Count
{
get { return ItemsInUse; }
}

public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}

public bool Remove(KeyValuePair<int, MyClass> item)
{
throw new NotImplementedException();
}

#endregion

#region IEnumerable<KeyValuePair<int,MyClass>> Members

public IEnumerator<KeyValuePair<int, MyClass>> GetEnumerator()
{
return -- what do I return here?
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}

#endregion
}


Thanks

Rich
 
R

Rich P

Just when I thought I was starting to get the hang of C# -- I stumble
onto this Iterator thing - custom collections, dictionaries... only to
be humbled (yet again :( ).

Thank you all for your replies. I will be studying the suggestions and
attempt to implement them.

Thanks all.

Rich
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top