How to use generics?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    How to use generics?

    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.Collecti ons.Generic;
    using System.Linq;
    using System.Text;

    namespace TestGeneric
    {
    class Program
    {
    ACollection<AAs = new ACollection<A>( );
    BCollection<BBs = 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.WriteLi ne("Hi my name is {0}", Name);
    }
    }

    public class A : BaseClass
    {
    }

    public class B : BaseClass
    {
    }

    public abstract class BaseDictionary< TValue: IDictionary<str ing,
    TValue>
    {
    #region IDictionary<str ing,TValueMembe rs

    public void Add(string key, TValue value)
    {
    throw new NotImplementedE xception();
    }

    public bool ContainsKey(str ing key)
    {
    throw new NotImplementedE xception();
    }

    public ICollection<str ingKeys
    {
    get { throw new NotImplementedE xception(); }
    }

    public bool Remove(string key)
    {
    throw new NotImplementedE xception();
    }

    public bool TryGetValue(str ing key, out TValue value)
    {
    throw new NotImplementedE xception();
    }

    public ICollection<TVa lueValues
    {
    get { throw new NotImplementedE xception(); }
    }

    public TValue this[string key]
    {
    get
    {
    throw new NotImplementedE xception();
    }
    set
    {
    throw new NotImplementedE xception();
    }
    }

    #endregion

    #region ICollection<Key ValuePair<strin g,TValue>Member s

    public void Add(KeyValuePai r<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }

    public void Clear()
    {
    throw new NotImplementedE xception();
    }

    public bool Contains(KeyVal uePair<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }

    public void CopyTo(KeyValue Pair<string, TValue>[] array, int
    arrayIndex)
    {
    throw new NotImplementedE xception();
    }

    public int Count
    {
    get { throw new NotImplementedE xception(); }
    }

    public bool IsReadOnly
    {
    get { throw new NotImplementedE xception(); }
    }

    public bool Remove(KeyValue Pair<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }

    #endregion

    #region IEnumerable<Key ValuePair<strin g,TValue>Member s

    public IEnumerator<Key ValuePair<strin g, TValue>>
    GetEnumerator()
    {
    throw new NotImplementedE xception();
    }

    #endregion

    #region IEnumerable Members

    System.Collecti ons.IEnumerator
    System.Collecti ons.IEnumerable .GetEnumerator( )
    {
    throw new NotImplementedE xception();
    }

    #endregion
    }

    public class ACollection<A: BaseDictionary< A>
    {
    }

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

  • Cowboy \(Gregory A. Beamer\)

    #2
    Re: How to use generics?

    If you want this to be more generic, there is no reason to create
    ACollection<Aan d BCollection<Bas 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<Twou ld 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


    or just read it:


    *************** *************** **************
    | Think outside the box! |
    *************** *************** **************
    "CSharper" <csharper@gmx.c omwrote in message
    news:41b5d400-c95d-424e-8baf-8be8f3658afa@p5 9g2000hsd.googl egroups.com...
    >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.Collecti ons.Generic;
    using System.Linq;
    using System.Text;
    >
    namespace TestGeneric
    {
    class Program
    {
    ACollection<AAs = new ACollection<A>( );
    BCollection<BBs = 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.WriteLi ne("Hi my name is {0}", Name);
    }
    }
    >
    public class A : BaseClass
    {
    }
    >
    public class B : BaseClass
    {
    }
    >
    public abstract class BaseDictionary< TValue: IDictionary<str ing,
    TValue>
    {
    #region IDictionary<str ing,TValueMembe rs
    >
    public void Add(string key, TValue value)
    {
    throw new NotImplementedE xception();
    }
    >
    public bool ContainsKey(str ing key)
    {
    throw new NotImplementedE xception();
    }
    >
    public ICollection<str ingKeys
    {
    get { throw new NotImplementedE xception(); }
    }
    >
    public bool Remove(string key)
    {
    throw new NotImplementedE xception();
    }
    >
    public bool TryGetValue(str ing key, out TValue value)
    {
    throw new NotImplementedE xception();
    }
    >
    public ICollection<TVa lueValues
    {
    get { throw new NotImplementedE xception(); }
    }
    >
    public TValue this[string key]
    {
    get
    {
    throw new NotImplementedE xception();
    }
    set
    {
    throw new NotImplementedE xception();
    }
    }
    >
    #endregion
    >
    #region ICollection<Key ValuePair<strin g,TValue>Member s
    >
    public void Add(KeyValuePai r<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }
    >
    public void Clear()
    {
    throw new NotImplementedE xception();
    }
    >
    public bool Contains(KeyVal uePair<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }
    >
    public void CopyTo(KeyValue Pair<string, TValue>[] array, int
    arrayIndex)
    {
    throw new NotImplementedE xception();
    }
    >
    public int Count
    {
    get { throw new NotImplementedE xception(); }
    }
    >
    public bool IsReadOnly
    {
    get { throw new NotImplementedE xception(); }
    }
    >
    public bool Remove(KeyValue Pair<string, TValueitem)
    {
    throw new NotImplementedE xception();
    }
    >
    #endregion
    >
    #region IEnumerable<Key ValuePair<strin g,TValue>Member s
    >
    public IEnumerator<Key ValuePair<strin g, TValue>>
    GetEnumerator()
    {
    throw new NotImplementedE xception();
    }
    >
    #endregion
    >
    #region IEnumerable Members
    >
    System.Collecti ons.IEnumerator
    System.Collecti ons.IEnumerable .GetEnumerator( )
    {
    throw new NotImplementedE xception();
    }
    >
    #endregion
    }
    >
    public class ACollection<A: BaseDictionary< A>
    {
    }
    >
    public class BCollection<B: BaseDictionary< B>
    {
    }
    }
    >

    Comment

    • CSharper

      #3
      Re: How to use generics?

      On Oct 9, 8:25 am, "Cowboy \(Gregory A. Beamer\)"
      <NoSpamMgbwo... @comcast.netNoS pamMwrote:
      If you want this to be more generic, there is no reason to create
      ACollection<Aan d BCollection<Bas 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<Twou ld 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.feedburne r.com/GregoryBeamer#
      >
      or just read it:http://feeds.feedburner.com/GregoryBeamer
      >
      *************** *************** **************
      | Think outside the box!                               |
      *************** *************** **************" CSharper" <cshar...@gmx.c om>wrote in message
      >
      news:41b5d400-c95d-424e-8baf-8be8f3658afa@p5 9g2000hsd.googl egroups.com...
      >
      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.Collecti ons.Generic;
      using System.Linq;
      using System.Text;
      >
      namespace TestGeneric
      {
         class Program
         {
             ACollection<AAs = new ACollection<A>( );
             BCollection<BBs = 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.WriteLi ne("Hi my name is {0}", Name);
             }
         }
      >
         public class A : BaseClass
         {
         }
      >
         public class B : BaseClass
         {
         }
      >
         public abstract class BaseDictionary< TValue: IDictionary<str ing,
      TValue>
         {
             #region IDictionary<str ing,TValueMembe rs
      >
             public void Add(string key, TValue value)
             {
                 throw new NotImplementedE xception();
             }
      >
             public bool ContainsKey(str ing key)
             {
                 throw new NotImplementedE xception();
             }
      >
             public ICollection<str ingKeys
             {
                 get { throw new NotImplementedE xception(); }
             }
      >
             public bool Remove(string key)
             {
                 throw new NotImplementedE xception();
             }
      >
             public bool TryGetValue(str ing key, out TValue value)
             {
                 throw new NotImplementedE xception();
             }
      >
             public ICollection<TVa lueValues
             {
                 get { throw new NotImplementedE xception(); }
             }
      >
             public TValue this[string key]
             {
                 get
                 {
                     throw new NotImplementedE xception();
                 }
                 set
                 {
                     throw new NotImplementedE xception();
                 }
             }
      >
             #endregion
      >
             #region ICollection<Key ValuePair<strin g,TValue>Member s
      >
             public void Add(KeyValuePai r<string, TValueitem)
             {
                 throw new NotImplementedE xception();
             }
      >
             public void Clear()
             {
                 throw new NotImplementedE xception();
             }
      >
             public bool Contains(KeyVal uePair<string, TValueitem)
             {
                 throw new NotImplementedE xception();
             }
      >
             public void CopyTo(KeyValue Pair<string, TValue>[] array,int
      arrayIndex)
             {
                 throw new NotImplementedE xception();
             }
      >
             public int Count
             {
                 get { throw new NotImplementedE xception(); }
             }
      >
             public bool IsReadOnly
             {
                 get { throw new NotImplementedE xception(); }
             }
      >
             public bool Remove(KeyValue Pair<string, TValueitem)
             {
                 throw new NotImplementedE xception();
             }
      >
             #endregion
      >
             #region IEnumerable<Key ValuePair<strin g,TValue>Member s
      >
             public IEnumerator<Key ValuePair<strin g, TValue>>
      GetEnumerator()
             {
                 throw new NotImplementedE xception();
             }
      >
             #endregion
      >
             #region IEnumerable Members
      >
             System.Collecti ons.IEnumerator
      System.Collecti ons.IEnumerable .GetEnumerator( )
             {
                 throw new NotImplementedE xception();
             }
      >
             #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.

      Comment

      • Hans Kesting

        #4
        Re: How to use generics?


        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.Collecti ons.Generic;
        using System.Linq;
        using System.Text;
        >
        namespace TestGeneric
        {
        class Program
        {
        ACollection<AAs = new ACollection<A>( );
        BCollection<BBs = 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< Aand 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


        Comment

        • CSharper

          #5
          Re: How to use generics?

          On Oct 9, 8:26 am, Hans Kesting <news.han...@sp amgourmet.comwr ote:
          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.Collecti ons.Generic;
          using System.Linq;
          using System.Text;
          >
          namespace TestGeneric
          {
              class Program
              {
                  ACollection<AAs = new ACollection<A>( );
                  BCollection<BBs = 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< Aand 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.

          Comment

          Working...