C
CSharper
I have a sample as shown in the bottom on the mail. I am trying to see
without changing anything in the way the As and Bs handled. I want to
make it DRY using generics. I am really interested in the
AddCollection method, I see the code repeatation just because the
collection is different type. Any thoughts?
Thanks,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGeneric
{
class Program
{
ACollection<A> As = new ACollection<A>();
BCollection<B> Bs = new BCollection<B>();
static void Main(string[] args)
{
Program pg = new Program();
}
public void AddCollection()
{
AddAs();
AddBs();
}
private void AddBs()
{
Bs.Add("Mary", new B() { Name = "Mary", Age = 30 });
Bs.Add("Helen", new B() { Name = "Helen", Age = 40 });
}
private void AddAs()
{
As.Add("Jim", new A() { Name = "Jim", Age = 30 });
As.Add("John", new A() { Name = "John", Age = 40 });
}
}
public abstract class BaseClass
{
public virtual string Name;
public virtual int Age;
public virtual void ShowName()
{
Console.WriteLine("Hi my name is {0}", Name);
}
}
public class A : BaseClass
{
}
public class B : BaseClass
{
}
public abstract class BaseDictionary<TValue> : IDictionary<string,
TValue>
{
#region IDictionary<string,TValue> Members
public void Add(string key, TValue value)
{
throw new NotImplementedException();
}
public bool ContainsKey(string key)
{
throw new NotImplementedException();
}
public ICollection<string> Keys
{
get { throw new NotImplementedException(); }
}
public bool Remove(string key)
{
throw new NotImplementedException();
}
public bool TryGetValue(string key, out TValue value)
{
throw new NotImplementedException();
}
public ICollection<TValue> Values
{
get { throw new NotImplementedException(); }
}
public TValue this[string key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
#endregion
#region ICollection<KeyValuePair<string,TValue>> Members
public void Add(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<string, TValue>[] array, int
arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable<KeyValuePair<string,TValue>> Members
public IEnumerator<KeyValuePair<string, TValue>>
GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
public class ACollection<A> : BaseDictionary<A>
{
}
public class BCollection<B> : BaseDictionary<B>
{
}
}
without changing anything in the way the As and Bs handled. I want to
make it DRY using generics. I am really interested in the
AddCollection method, I see the code repeatation just because the
collection is different type. Any thoughts?
Thanks,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGeneric
{
class Program
{
ACollection<A> As = new ACollection<A>();
BCollection<B> Bs = new BCollection<B>();
static void Main(string[] args)
{
Program pg = new Program();
}
public void AddCollection()
{
AddAs();
AddBs();
}
private void AddBs()
{
Bs.Add("Mary", new B() { Name = "Mary", Age = 30 });
Bs.Add("Helen", new B() { Name = "Helen", Age = 40 });
}
private void AddAs()
{
As.Add("Jim", new A() { Name = "Jim", Age = 30 });
As.Add("John", new A() { Name = "John", Age = 40 });
}
}
public abstract class BaseClass
{
public virtual string Name;
public virtual int Age;
public virtual void ShowName()
{
Console.WriteLine("Hi my name is {0}", Name);
}
}
public class A : BaseClass
{
}
public class B : BaseClass
{
}
public abstract class BaseDictionary<TValue> : IDictionary<string,
TValue>
{
#region IDictionary<string,TValue> Members
public void Add(string key, TValue value)
{
throw new NotImplementedException();
}
public bool ContainsKey(string key)
{
throw new NotImplementedException();
}
public ICollection<string> Keys
{
get { throw new NotImplementedException(); }
}
public bool Remove(string key)
{
throw new NotImplementedException();
}
public bool TryGetValue(string key, out TValue value)
{
throw new NotImplementedException();
}
public ICollection<TValue> Values
{
get { throw new NotImplementedException(); }
}
public TValue this[string key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
#endregion
#region ICollection<KeyValuePair<string,TValue>> Members
public void Add(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<string, TValue>[] array, int
arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(KeyValuePair<string, TValue> item)
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable<KeyValuePair<string,TValue>> Members
public IEnumerator<KeyValuePair<string, TValue>>
GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
public class ACollection<A> : BaseDictionary<A>
{
}
public class BCollection<B> : BaseDictionary<B>
{
}
}