Re: How to use generics?
- From: "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 9 Oct 2008 08:25:00 -0500
If you want this to be more generic, there is no reason to create ACollection<A> and BCollection<B> as both go to BaseDictionary<T>. Remove the specific implementation, which is nothing more than a naming token, as it stands.
If you are talking the particular code, you have the following classes:
public class A
{
public string Name { get; set; }
public int Age { get; set; }
}
public class B
{
public string Name { get; set; }
public int Age { get; set; }
}
I assume this is just a learning exercise, as these are identical classes. If I found this type of construct in real software, I would get rid of one of them, unless there was some form of additional adornment on one of the classes, then I would derive. Example:
public class A
{
public string Name { get; set; }
public int Age { get; set; }
}
public class B : A
{
public int EmployeeId { get; set; }
}
If you want this to be more dry, create a handler class that can also take A or B. You can do the same to genericize the methods. But, getting rid of the ACollection and BCollection and just having Collection<T> would be a good start towards using generics to the full extent.
Hope this helps!
--
Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA
Subscribe to my blog
http://feeds.feedburner.com/GregoryBeamer#
or just read it:
http://feeds.feedburner.com/GregoryBeamer
********************************************
| Think outside the box! |
********************************************
"CSharper" <csharper@xxxxxxx> wrote in message news:41b5d400-c95d-424e-8baf-8be8f3658afa@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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>
{
}
}
.
- Follow-Ups:
- Re: How to use generics?
- From: CSharper
- Re: How to use generics?
- References:
- How to use generics?
- From: CSharper
- How to use generics?
- Prev by Date: Re: In .xml parsing how to skip subnodes and move to next node using XmlTextReader
- Next by Date: Re: close() and dispose()
- Previous by thread: How to use generics?
- Next by thread: Re: How to use generics?
- Index(es):
Relevant Pages
|
Loading