Re: How to use generics?



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


.



Relevant Pages

  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Tricky form problem
    ... > ShowDialog method) different modal child forms. ... when using ShowDialog() there's no ... > public class MainForm: Form ... > public void ShowForm() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Casting Generic Classes - Possible Solution
    ... It leaves me with the open option to use generics or not when I ... This works as saying "accept a store of anything that are Record ... public class Record { ... So I tried adding an interface into the structure: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: InvalidOperationException was unhandled
    ... > public Vehicle MyVehicle; ... >public class Vehicle ... > public void SerializeOriginal(String filename) ...
    (microsoft.public.dotnet.vjsharp)
  • RE: Problem: C# custom event is null
    ... public event MyDelegate OnFire; ... public void B_Event_Raised ... (these are sample classes to illustrate) ...
    (microsoft.public.dotnet.languages.csharp)

Loading