How to use generics?

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

Cowboy \(Gregory A. Beamer\)

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 said:
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>
{
}
}
 
H

Hans Kesting

CSharper was thinking very hard :
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>();
[snip]
public class ACollection<A> : BaseDictionary<A>
{
}

public class BCollection<B> : BaseDictionary<B>
{
}

Do you want to do anything special in ACollection or BCollection?

If not, forget the ACollection and BCollection, use just
BaseDictionary<A> and BaseDictionary<B>.

By declaring 'public class ACollection<A>' you don't specify your own
class "A" as the type, but you declare a new generic type with an
as-yet unspecified type that you happened to call "A".

Hans Kesting
 
C

CSharper

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 bloghttp://feeds.feedburner.com/GregoryBeamer#

or just read it:http://feeds.feedburner.com/GregoryBeamer

********************************************
| Think outside the box!                               |

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?

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

Thank you for the input. In this case, I would like to keep A and B as
it is to get the DSL look for anyone who would use it. I hope I didn't
confuse you.
 
C

CSharper

CSharper was thinking very hard :


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?

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>();
[snip]
   public class ACollection<A> : BaseDictionary<A>
   {
   }
   public class BCollection<B> : BaseDictionary<B>
   {
   }

Do you want to do anything special in ACollection or BCollection?

If not, forget the ACollection and BCollection, use just
BaseDictionary<A> and BaseDictionary<B>.

By declaring 'public class ACollection<A>' you don't specify your own
class "A" as the type, but you declare a new generic type with an
as-yet unspecified type that you happened to call "A".

Hans Kesting

Thanks both for the suggestions.
 

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