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
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