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. Generics Problems

Generics Problems

Scheduled Pinned Locked Moved C#
helpquestioncsharpwinforms
24 Posts 7 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.
  • E Ennis Ray Lynch Jr

    Why bother with generics if you can't use them? You are better off doing things in a straight forward manner twice, than once in a non-obvious manner.

    setProperty("Name", "The Name");
    setProperty("LinkedToBilling", true);
    setProperty("BillingStartDate", new DateTime(2012, 01, 15));

    May look like a nice, elegant solution, until you realize that

    setProperty("Name", "The Name");
    setProperty("LinkedToBiling", true);
    setProperty("BillingStartDate", new DateTime(2012, 01, 15));

    doesn't give a compiler error.

    Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

    C Offline
    C Offline
    Clifford Nelson
    wrote on last edited by
    #13

    I have to agree. There appears to be no reason to use generics.

    1 Reply Last reply
    0
    • K Kevin Marois

      I'm refactoring a WinForms project. I have 2 forms that do the same thing, except the data entities passed in are slightly different, so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them. Assume I have these 2 entites:

      public class Entity1
      {
      public string SomeProperty { get; set; }
      public string Name { get; set; }
      }
      public class Entity2
      {
      public string SomeProperty { get; set; }
      public string Caption { get; set; }
      }

      In the CTOR of the form I have:

      public partial class Form1 : Form
      {
      T entity;

      public Form1()
      {
          InitializeComponent();
      
          entity = (T)Activator.CreateInstance(typeof(T));
      
      }
      

      }

      The problem is I now need to set properties on the object passed in. In most cases the 2 objects have the same name properties, but ther are differences. What is the right way to work with this object? Seems like I have no choice but to cast. Thanks

      If it's not broken, fix it until it is

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

      Right-click the "Caption" property, choose "Refactor / Rename"?

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      K 1 Reply Last reply
      0
      • K Kevin Marois

        I'm refactoring a WinForms project. I have 2 forms that do the same thing, except the data entities passed in are slightly different, so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them. Assume I have these 2 entites:

        public class Entity1
        {
        public string SomeProperty { get; set; }
        public string Name { get; set; }
        }
        public class Entity2
        {
        public string SomeProperty { get; set; }
        public string Caption { get; set; }
        }

        In the CTOR of the form I have:

        public partial class Form1 : Form
        {
        T entity;

        public Form1()
        {
            InitializeComponent();
        
            entity = (T)Activator.CreateInstance(typeof(T));
        
        }
        

        }

        The problem is I now need to set properties on the object passed in. In most cases the 2 objects have the same name properties, but ther are differences. What is the right way to work with this object? Seems like I have no choice but to cast. Thanks

        If it's not broken, fix it until it is

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #15

        Kevin Marois wrote:

        so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them.

        Obviously a good decision by that person because they are in fact different. The fact that there seem to be common components between them is NOT sufficient reason to assume that they are the same. As an example the fact that Word and Visual Studio both allow me to type text doesn't mean that those two code bases should be merged.

        Kevin Marois wrote:

        What is the right way to work with this object?

        The right way is to re-assess the decision to combine them in the first place. Nothing you have posted here suggests that they should be combined. And in fact what has been posted would suggest that they should NOT be combined. Of course maybe there is some other factor that makes it obvious.

        K 1 Reply Last reply
        0
        • J jschell

          Kevin Marois wrote:

          so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them.

          Obviously a good decision by that person because they are in fact different. The fact that there seem to be common components between them is NOT sufficient reason to assume that they are the same. As an example the fact that Word and Visual Studio both allow me to type text doesn't mean that those two code bases should be merged.

          Kevin Marois wrote:

          What is the right way to work with this object?

          The right way is to re-assess the decision to combine them in the first place. Nothing you have posted here suggests that they should be combined. And in fact what has been posted would suggest that they should NOT be combined. Of course maybe there is some other factor that makes it obvious.

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #16

          jschell wrote:

          Obviously a good decision by that person because they are in fact different.
           
          The fact that there seem to be common components between them is NOT sufficient reason to assume that they are the same. As an example the fact that Word and Visual Studio both allow me to type text doesn't mean that those two code bases should be merged.

          This is an entirely stupid argument. You're saying it's ok to have 2 entirely duplicate forms and their code becuase the data they work with is different.

          jschell wrote:

          The right way is to re-assess the decision to combine them in the first place. Nothing you have posted here suggests that they should be combined. And in fact what has been posted would suggest that they should NOT be combined.

          Again, same form, same code, different data - There is NO good reason to have 2 identical forms.

          If it's not broken, fix it until it is

          J 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            I wouldn't use it but you can use the wrapper pattern below. It isn't elegant, but is a way to handle what you want without changing the underlying objects using interfaces, which really is probably the best solution.

            public class Wrapper{
            private Object mObject;
            public string Name{
            get{
            string result = null;
            if(mObject is SomeTypeIRecognize){
            result = ((SomeTypeIRecognize)mObject).Name;
            }
            return result;

                }
            }
            
            public Wrapper(object o){mObject = o;}
            

            }

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            K Offline
            K Offline
            Kevin Marois
            wrote on last edited by
            #17

            That is exaclty what I'm trying to avoid - lots of IF blocks all over the place. The generic & reflection code I posted will work fine.

            If it's not broken, fix it until it is

            1 Reply Last reply
            0
            • L Lost User

              Right-click the "Caption" property, choose "Refactor / Rename"?

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #18

              Um, whaaa?

              If it's not broken, fix it until it is

              L 1 Reply Last reply
              0
              • K Kevin Marois

                Um, whaaa?

                If it's not broken, fix it until it is

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

                public class Entity1
                {
                public string SomeProperty { get; set; }
                public string Name { get; set; }
                }
                public class Entity2
                {
                public string SomeProperty { get; set; }
                public string Name { get; set; }
                }

                Looks like the same two classes to me. What did I miss?

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                K 1 Reply Last reply
                0
                • L Lost User

                  public class Entity1
                  {
                  public string SomeProperty { get; set; }
                  public string Name { get; set; }
                  }
                  public class Entity2
                  {
                  public string SomeProperty { get; set; }
                  public string Name { get; set; }
                  }

                  Looks like the same two classes to me. What did I miss?

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  K Offline
                  K Offline
                  Kevin Marois
                  wrote on last edited by
                  #20

                  You missed that these are data entites that are both passed to the same form. The code in the forms is identical, except where the data is concerned. There's no good reason to have 2 forms.

                  If it's not broken, fix it until it is

                  L 1 Reply Last reply
                  0
                  • K Kevin Marois

                    You missed that these are data entites that are both passed to the same form. The code in the forms is identical, except where the data is concerned. There's no good reason to have 2 forms.

                    If it's not broken, fix it until it is

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

                    I see. And you need to set them only once? I mean, you could expose two properties on the form, used to set the data. If the code treats them as being identical, then why not assign those two strings?

                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                    1 Reply Last reply
                    0
                    • K Kevin Marois

                      I'm refactoring a WinForms project. I have 2 forms that do the same thing, except the data entities passed in are slightly different, so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them. Assume I have these 2 entites:

                      public class Entity1
                      {
                      public string SomeProperty { get; set; }
                      public string Name { get; set; }
                      }
                      public class Entity2
                      {
                      public string SomeProperty { get; set; }
                      public string Caption { get; set; }
                      }

                      In the CTOR of the form I have:

                      public partial class Form1 : Form
                      {
                      T entity;

                      public Form1()
                      {
                          InitializeComponent();
                      
                          entity = (T)Activator.CreateInstance(typeof(T));
                      
                      }
                      

                      }

                      The problem is I now need to set properties on the object passed in. In most cases the 2 objects have the same name properties, but ther are differences. What is the right way to work with this object? Seems like I have no choice but to cast. Thanks

                      If it's not broken, fix it until it is

                      D Offline
                      D Offline
                      dbaseman
                      wrote on last edited by
                      #22

                      The best way to do this is to create an interface that encapsulates the common properties of your entities:

                      public interface IEntity
                      {
                      string SomeProperty { get; set; }
                      }

                      public interface IAnotherInterface
                      {
                      string Name { get; set; }
                      }

                      Each entity can inherit multiple interfaces, depending on how you want it set up:

                      public class Entity1 : IEntity, IAnotherInterface
                      {
                      public string SomeProperty { get; set; }
                      public string Name { get; set; }
                      }

                      If every entity has a certain interface, then you can require it in the Form definition:

                      public partial class Form1 : Form
                      where T : IEntity

                      Finally, cast to the interfaces to set properties. If only some entities implement a given interface, check first.

                      void SetSomeProperties()
                      {
                      (entity as IEntity).SomeProperty = "foo";
                      if (entity is IAnotherInterface)
                      {
                      (entity as IAnotherInterface).Name = "bar";
                      }
                      }

                      1 Reply Last reply
                      0
                      • K Kevin Marois

                        jschell wrote:

                        Obviously a good decision by that person because they are in fact different.
                         
                        The fact that there seem to be common components between them is NOT sufficient reason to assume that they are the same. As an example the fact that Word and Visual Studio both allow me to type text doesn't mean that those two code bases should be merged.

                        This is an entirely stupid argument. You're saying it's ok to have 2 entirely duplicate forms and their code becuase the data they work with is different.

                        jschell wrote:

                        The right way is to re-assess the decision to combine them in the first place. Nothing you have posted here suggests that they should be combined. And in fact what has been posted would suggest that they should NOT be combined.

                        Again, same form, same code, different data - There is NO good reason to have 2 identical forms.

                        If it's not broken, fix it until it is

                        J Offline
                        J Offline
                        jschell
                        wrote on last edited by
                        #23

                        Kevin Marois wrote:

                        This is an entirely stupid argument.

                        Nope. I have seen countless attempts by people to combine code based on nothing more than what they perceive as code duplication. That happens a lot with inheritance. It focuses on the duplication instead of focusing on the need.

                        Kevin Marois wrote:

                        You're saying it's ok to have 2 entirely duplicate forms and their code becuase the data they work with is different

                        What I said was that from what you posted here that is the case.

                        Kevin Marois wrote:

                        There is NO good reason to have 2 identical forms.

                        Actually there can be a very good reason. Because they represent two different conceptual entities. Because they are different it can mean that in the future they might go in different directions. Again code duplication is not the sole criteria that one uses to make this decision. And I gave you a specific example which would demonstrate why one wouldn't do that. Without more information all I can suppose is that what you really need, if indeed there is a real need at all, is a helper class. It contains the duplicated code. The current implementations would use that. That however still supposes that there is a real need to combine this code.

                        1 Reply Last reply
                        0
                        • K Kevin Marois

                          I'm refactoring a WinForms project. I have 2 forms that do the same thing, except the data entities passed in are slightly different, so the guy who wrote them decided to duplicate the forms instead of making one form that works for both. So I'm refactoring them. Assume I have these 2 entites:

                          public class Entity1
                          {
                          public string SomeProperty { get; set; }
                          public string Name { get; set; }
                          }
                          public class Entity2
                          {
                          public string SomeProperty { get; set; }
                          public string Caption { get; set; }
                          }

                          In the CTOR of the form I have:

                          public partial class Form1 : Form
                          {
                          T entity;

                          public Form1()
                          {
                              InitializeComponent();
                          
                              entity = (T)Activator.CreateInstance(typeof(T));
                          
                          }
                          

                          }

                          The problem is I now need to set properties on the object passed in. In most cases the 2 objects have the same name properties, but ther are differences. What is the right way to work with this object? Seems like I have no choice but to cast. Thanks

                          If it's not broken, fix it until it is

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

                          Create an abstract Form class that contains all the "shared" code; then create 2 Forms that contain only the code / properties that are unique and inherit (are subclassed from) the abstract Form. 3 Forms, but simpler and cleaner than generics, reflection, (probably less code), etc. (IMO)

                          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