Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generic usage is not clear in one program

Generic usage is not clear in one program

Scheduled Pinned Locked Moved C#
questioncsshelp
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tridip Bhattacharjee
    wrote on last edited by
    #1

    see this class code

    public class ShipperFactory
    {
    private static TShip Create()
    where TShip : IShip, // interface contract constraint
    new() // public parameter-less constructor constraint
    {
    return new TShip();
    }

        public static readonly IDictionary\> Creators =
            new Dictionary\>()
        {
            { Shipper.UPS, () => Create() },
            { Shipper.FedEx, () => Create() },
            { Shipper.Purolator, () => Create() }
        };
    
        public static IShip CreateInstance(Shipper enumModuleName)
        {
            return Creators\[enumModuleName\]();
        }
    }
    

    specially the below code is not clear where

    public static readonly IDictionary> Creators =
    new Dictionary>()
    {
    { Shipper.UPS, () => Create() },
    { Shipper.FedEx, () => Create() },
    { Shipper.Purolator, () => Create() }
    };

    1. see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------

    private void btnUPS_Click(object sender, EventArgs e)
    {
    ShipperFactory.CreateInstance(Shipper.UPS).Ship();
    }

        private void btnFedEx\_Click(object sender, EventArgs e)
        {
            ShipperFactory.CreateInstance(Shipper.FedEx).Ship();
        }
    
        private void btnPurolator\_Click(object sender, EventArgs e)
        {
            ShipperFactory.CreateInstance(Shipper.Purolator).Ship();
        }
    
    public enum Shipper
    {
        UPS,
        FedEx,
        Purolator
    }
    
    public interface IShip
    {
        void Ship();
    }
    
    public abstract class ShipperBase : IShip
    {
        //Etc
        public abstract void Ship();
    
    }
    
    public class ShipperUPS : ShipperBase
    {
        public override void Ship()
        {
            //-- cod
    
    L 2 Replies Last reply
    0
    • T Tridip Bhattacharjee

      see this class code

      public class ShipperFactory
      {
      private static TShip Create()
      where TShip : IShip, // interface contract constraint
      new() // public parameter-less constructor constraint
      {
      return new TShip();
      }

          public static readonly IDictionary\> Creators =
              new Dictionary\>()
          {
              { Shipper.UPS, () => Create() },
              { Shipper.FedEx, () => Create() },
              { Shipper.Purolator, () => Create() }
          };
      
          public static IShip CreateInstance(Shipper enumModuleName)
          {
              return Creators\[enumModuleName\]();
          }
      }
      

      specially the below code is not clear where

      public static readonly IDictionary> Creators =
      new Dictionary>()
      {
      { Shipper.UPS, () => Create() },
      { Shipper.FedEx, () => Create() },
      { Shipper.Purolator, () => Create() }
      };

      1. see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------

      private void btnUPS_Click(object sender, EventArgs e)
      {
      ShipperFactory.CreateInstance(Shipper.UPS).Ship();
      }

          private void btnFedEx\_Click(object sender, EventArgs e)
          {
              ShipperFactory.CreateInstance(Shipper.FedEx).Ship();
          }
      
          private void btnPurolator\_Click(object sender, EventArgs e)
          {
              ShipperFactory.CreateInstance(Shipper.Purolator).Ship();
          }
      
      public enum Shipper
      {
          UPS,
          FedEx,
          Purolator
      }
      
      public interface IShip
      {
          void Ship();
      }
      
      public abstract class ShipperBase : IShip
      {
          //Etc
          public abstract void Ship();
      
      }
      
      public class ShipperUPS : ShipperBase
      {
          public override void Ship()
          {
              //-- cod
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The dictionary contains a "key" for each carrier and a "function" (value) that is used to create an instance of that carrier. Over-engineered and pointless indirection (IMO). (A "carrier" class and a "carrier id / ident" would be simpler).

      "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

      OriginalGriffO 1 Reply Last reply
      0
      • L Lost User

        The dictionary contains a "key" for each carrier and a "function" (value) that is used to create an instance of that carrier. Over-engineered and pointless indirection (IMO). (A "carrier" class and a "carrier id / ident" would be simpler).

        "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        DNFTHV * :laugh: * Do not feed the Help Vampire

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        L 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          DNFTHV * :laugh: * Do not feed the Help Vampire

          Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          It's actually passive aggression :cool:

          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

          OriginalGriffO 1 Reply Last reply
          0
          • L Lost User

            It's actually passive aggression :cool:

            "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            :laugh: Some of these don't deserve "passive" - they just don't want to think for themselves...

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            1 Reply Last reply
            0
            • T Tridip Bhattacharjee

              see this class code

              public class ShipperFactory
              {
              private static TShip Create()
              where TShip : IShip, // interface contract constraint
              new() // public parameter-less constructor constraint
              {
              return new TShip();
              }

                  public static readonly IDictionary\> Creators =
                      new Dictionary\>()
                  {
                      { Shipper.UPS, () => Create() },
                      { Shipper.FedEx, () => Create() },
                      { Shipper.Purolator, () => Create() }
                  };
              
                  public static IShip CreateInstance(Shipper enumModuleName)
                  {
                      return Creators\[enumModuleName\]();
                  }
              }
              

              specially the below code is not clear where

              public static readonly IDictionary> Creators =
              new Dictionary>()
              {
              { Shipper.UPS, () => Create() },
              { Shipper.FedEx, () => Create() },
              { Shipper.Purolator, () => Create() }
              };

              1. see this line whose meaning is not clea. what is the meaning of Func ? new Dictionary>() Dictionary usage is not clear. help me to understand the code of Dictionary and as well as ShipperFactory class. it is required to use both interface and abstract class ? is it not redundant here ? here giving the full code which show how i am using ShipperFactory class calling like this way --------------------------

              private void btnUPS_Click(object sender, EventArgs e)
              {
              ShipperFactory.CreateInstance(Shipper.UPS).Ship();
              }

                  private void btnFedEx\_Click(object sender, EventArgs e)
                  {
                      ShipperFactory.CreateInstance(Shipper.FedEx).Ship();
                  }
              
                  private void btnPurolator\_Click(object sender, EventArgs e)
                  {
                      ShipperFactory.CreateInstance(Shipper.Purolator).Ship();
                  }
              
              public enum Shipper
              {
                  UPS,
                  FedEx,
                  Purolator
              }
              
              public interface IShip
              {
                  void Ship();
              }
              
              public abstract class ShipperBase : IShip
              {
                  //Etc
                  public abstract void Ship();
              
              }
              
              public class ShipperUPS : ShipperBase
              {
                  public override void Ship()
                  {
                      //-- cod
              
              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Tridip Bhattacharjee wrote:

              looking for help

              Try asking the person who wrote the code.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups