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. Multiple Classes, same name

Multiple Classes, same name

Scheduled Pinned Locked Moved C#
csharpquestionannouncement
69 Posts 15 Posters 1 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.
  • L Lost User

    Which has absolutely nothing to do with your question.

    W Offline
    W Offline
    Wiep Corbier
    wrote on last edited by
    #21

    That is true. It has to do with the fact you call it dumb. That isn't very constructive. But don't waste your time on me.

    OriginalGriffO L 2 Replies Last reply
    0
    • W Wiep Corbier

      That is true. It has to do with the fact you call it dumb. That isn't very constructive. But don't waste your time on me.

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

      Sometimes, the most constructive thing you can be told is "that is a dumb idea - don't waste any more of your time on it". And one of the hardest things we have to do as developers is recognise that fact about our own stuff and throw it away and start again. We all do it: I had a brilliant scheme laid out - pages and page of design documents, using abstract classes to their full extent: everything was beautiful, it would be perfect. Except ... C# doesn't have abstract static methods. And for very, very good reasons. So the whole lot: design, code, the whole idea was a pile of garbage, and I had to chuck it and start again. That really is hard to do, but it's part of the job. Yes, I could have bodged round the problem, hacked it here, forced it there - but the result would be clumsy, hard to work with, and difficult to maintain; as well as having a lot of "holes" for bugs to hide. Trust us: this is an idea you should drop!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

      "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

      W 1 Reply Last reply
      0
      • P Pete OHanlon

        I'm going to start by pointing out the most obvious thing. Your two POCO's have different implementations. Bear with me, because this is important for the rest of the discussion. As these two POCO's have different properties, you have an immediate issue which is, what happens when you want to consume your code? You have Skills as a string in one, and a List of strings in the other. The code that calls this is going to have to conditionally choose the operations to perform on the Skills based on the "popup". Now, you might have additional operations that chain off those operations, so you might have a POCO Skill class that looks like this:

        public class Skill
        {
        public int SkillId { get; set; }
        public string SkillRange { get; set; }
        }

        and another one that looks like this:

        public class Skill
        {
        public int SkillId { get; set; }
        public List SkillRange { get; set; }
        }

        And maybe you have another class that looks like this

        public class Skill
        {
        public Guid SkillId { get; set; }
        public string SkillRange { get; set; }
        }

        For good measure, our codebase has this one thrown in

        public class Skill
        {
        public Guid SkillId { get; set; }
        public List SkillRange { get; set; }
        }

        We are creating a real mess of code here that we are expecting someone to be able to grok easily. The more we add to the code base, the harder it will be to keep these in line, and we have violated clear code principles. Why have we violated this? Suppose we have some consuming code that looks like this:

        Skill skill = GetPopulatedSkill();
        var id = skill.SkillId;
        ValidateSkillRange(skill.SkillRange);

        What validation are you expecting ValidateSkillRange to be doing? Is it a string.IsNullOrWhiteSpace check? Perhaps you just want to ensure it's not an empty list. Perhaps you need to iterate over items in the list and check certain values in there. The problem is, unless you start covering all of the different combinations of cases, the compiler isn't going to have the last scooby of a clue how to fix this, even if the user, by luck, picks the correct one. Bear in mind that you may have different parts of your code needing different Skill implementations at different points; you're expecting the user to be able to guess which one needs to appl

        W Offline
        W Offline
        Wiep Corbier
        wrote on last edited by
        #23

        Hi Pete, All these problems are already solved by me. Again. I store Skills as a string in the database. They are stored like this: Skill 1|Skill 2|Skill 3|Skill 4 of course the data is different but I show you the idea Then, customers (being developers) asked me if i couldn't provide a class with data stored in Skills as a list A list Skills has "items"

        public class Skill
        {
            public string Item { get; set; }
        }
        

        It doesn't matter if there is data in it, I handle that. So, I said to my customers I could do what the want but they need to address another Class:

        CandidateFunctionWithSkillsList

        No problem. But it made me think: what if my customers could make a choise how to recieve the data using the same name for the claas but had the option how it was presented/formatted. It is just something I would like. So I went on searching and discovered there were alternatives. But not exactly what I want. It doesn't exist. So, my question to the C# team is: can you make it happen in a next release.

        OriginalGriffO P K 3 Replies Last reply
        0
        • OriginalGriffO OriginalGriff

          Sometimes, the most constructive thing you can be told is "that is a dumb idea - don't waste any more of your time on it". And one of the hardest things we have to do as developers is recognise that fact about our own stuff and throw it away and start again. We all do it: I had a brilliant scheme laid out - pages and page of design documents, using abstract classes to their full extent: everything was beautiful, it would be perfect. Except ... C# doesn't have abstract static methods. And for very, very good reasons. So the whole lot: design, code, the whole idea was a pile of garbage, and I had to chuck it and start again. That really is hard to do, but it's part of the job. Yes, I could have bodged round the problem, hacked it here, forced it there - but the result would be clumsy, hard to work with, and difficult to maintain; as well as having a lot of "holes" for bugs to hide. Trust us: this is an idea you should drop!

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

          W Offline
          W Offline
          Wiep Corbier
          wrote on last edited by
          #24

          That is what car makers told Elon Musk. Trust us, your idea is never going to work. They said: Where do you get electricity? How do you store your energy source? What will be the range? Etc. No, the big shots told him: it will not work. Tesla is today the biggest car builder. If you think in unpossibilities you will never innovate.

          1 Reply Last reply
          0
          • W Wiep Corbier

            Richard, I know that what I want isn't possible. That's the reason why I post this. It is a nice to have for me and probably someone else. But what about something else like it: inheritance. Why? Are people to lazy to copy properties? inheritance is not a must. It is a nice to have. btw: I did predict you would mention the surname Richard? Which Richard? Richard String or Richard List? ps: just for fun.

            Z Offline
            Z Offline
            ZurdoDev
            wrote on last edited by
            #25

            Wiep Corbier wrote:

            It is a nice to have for me and probably someone else.

            Not really. Even if the feature were there, when you went back a day later and looked at your code you'd have no idea which class the code was referring to.

            Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

            1 Reply Last reply
            0
            • W Wiep Corbier

              That is true. It has to do with the fact you call it dumb. That isn't very constructive. But don't waste your time on me.

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

              Wiep Corbier wrote:

              It has to do with the fact you call it dumb.

              I don't call it dumb, it is dumb. And the argument re petrol or electric cars is not even remotely connected. Electric cars are all easily identified as different from their petrol counterparts.

              Wiep Corbier wrote:

              don't waste your time on me.

              At last a sensible suggestion.

              K 1 Reply Last reply
              0
              • Richard DeemingR Richard Deeming

                Wiep Corbier wrote:

                People will ask, which Richard?

                There has to be some way to disambiguate the two people. Usually you would use the surname. It's no different with classes. If you have two classes with the same name, then they have to be in different namespaces so that you can disambiguate them. Otherwise, even if you could come up with an intellisense popup to let you pick a class - which could be difficult if the only difference is one character on line 42! - there would be no way to represent that choice in the compiled program. And before you suggest that the compiler could generate some form of "mangled" name to uniquely identify the class, remember that you'd also have to represent that choice in the source code. And the source code is meant to be read by humans. There are enough arguments against using var when the type isn't clear, without making an explicit variable type ambiguous as well!

                Wiep Corbier wrote:

                C# (8?) now has the ability to work around null values in classes. Who asked for that? I didn't. Who cares? No one.

                Lots of people asked for it, and lots of people care. You can probably find the history in the C# Language Design repository: GitHub - dotnet/csharplang: The official repo for the design of the C# programming language[^]


                "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                Z Offline
                Z Offline
                ZurdoDev
                wrote on last edited by
                #27

                Richard Deeming wrote:

                There has to be some way to disambiguate the two people.

                I think the OP is asking for the computer to do that. In other words I write SomeClass temp = new SomeClass(); and up pops some intellisense so that I can pick SomeClass from file1 or SomeClass from file2. Internally, C# would clearly have to track which one it is. Which makes this idea even worse because if you come back the next day to read your code you won't know which one it actually refers to. It could be done, and probably not too hard with some sort of internal tracker in VS but a terrible idea regardless. :-D

                Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                1 Reply Last reply
                0
                • W Wiep Corbier

                  For the next C# version I would like the ability to have multiple classes with the same name. But, this is for POCO's What do I mean? I have an poco with the name CandidateFunction

                  public class CandidateFunction
                  {
                      public int CandidateFunctionId { get; set; }
                      public int CandidateId { get; set; }
                      public int FunctionId { get; set; }
                      public string Skills { get; set; }
                      public DateTime Date { get; set; }
                  }
                  

                  As you can see, the property with the name Skills is a string. It can be a long string and it stores one or more Skills. But customers want to see Skills as a list so I have another POCO

                  public class CandidateFunctionWithSkillsList
                  {
                      public int CandidateFunctionId { get; set; }
                      public int CandidateId { get; set; }
                      public int FunctionId { get; set; }
                      public List Skills { get; set; }
                      public DateTime Date { get; set; }
                  }
                  

                  It has another name. Now, I want both POCO's with the same name. I know there alternatives for what I want but I don't like them. So, when I instantiate a new CandidateFunction, I want a popup asking me which one I want to use. Will this ever happen and if not, why not?

                  Z Offline
                  Z Offline
                  ZurdoDev
                  wrote on last edited by
                  #28

                  Wiep Corbier wrote:

                  As you can see, the property with the name Skills is a string. It can be a long string and it stores one or more Skills.

                  Forget multiple classes with the same name, I'm wondering why in the world you would do this.

                  Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                  W 1 Reply Last reply
                  0
                  • Z ZurdoDev

                    Wiep Corbier wrote:

                    As you can see, the property with the name Skills is a string. It can be a long string and it stores one or more Skills.

                    Forget multiple classes with the same name, I'm wondering why in the world you would do this.

                    Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                    W Offline
                    W Offline
                    Wiep Corbier
                    wrote on last edited by
                    #29

                    Why? I just explained it several times.

                    Quote:

                    When answering a question please; Read the question carefully

                    Z 1 Reply Last reply
                    0
                    • W Wiep Corbier

                      Why? I just explained it several times.

                      Quote:

                      When answering a question please; Read the question carefully

                      Z Offline
                      Z Offline
                      ZurdoDev
                      wrote on last edited by
                      #30

                      Wiep Corbier wrote:

                      I just explained it several times.

                      Not in your OP. :doh:

                      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

                      1 Reply Last reply
                      0
                      • W Wiep Corbier

                        Hi Pete, All these problems are already solved by me. Again. I store Skills as a string in the database. They are stored like this: Skill 1|Skill 2|Skill 3|Skill 4 of course the data is different but I show you the idea Then, customers (being developers) asked me if i couldn't provide a class with data stored in Skills as a list A list Skills has "items"

                        public class Skill
                        {
                            public string Item { get; set; }
                        }
                        

                        It doesn't matter if there is data in it, I handle that. So, I said to my customers I could do what the want but they need to address another Class:

                        CandidateFunctionWithSkillsList

                        No problem. But it made me think: what if my customers could make a choise how to recieve the data using the same name for the claas but had the option how it was presented/formatted. It is just something I would like. So I went on searching and discovered there were alternatives. But not exactly what I want. It doesn't exist. So, my question to the C# team is: can you make it happen in a next release.

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

                        Wiep Corbier wrote:

                        I store Skills as a string in the database. They are stored like this: Skill 1|Skill 2|Skill 3|Skill 4

                        And that all on it's own is a poor idea. Do you realise how much work it is in SQL to change a skill when you store it like that? First off, it wastes space, secondly it's prone to user error and two skills that are described differently but are actually the same, and thirdly because it's hard to work with for anything more complex than INSERT and DELETE operations. For example, suppose HR realises a mistake has been made: EmployeeX doesn't have any knowledge in "Skill 2", it's "Skill 5" he knows. Now write the SQL to fix that ... and bear in mind that you might have "Skill 2", and "Skill 21", "Skill 22", "Skill 23", ... so you can't rely on string replace functions or you will cause further errors. Oh, and while you are at it, fix the spelling mistake that Deirdre always makes in "Skill 17" but no one else does.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                        "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

                        W 1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          Wiep Corbier wrote:

                          I store Skills as a string in the database. They are stored like this: Skill 1|Skill 2|Skill 3|Skill 4

                          And that all on it's own is a poor idea. Do you realise how much work it is in SQL to change a skill when you store it like that? First off, it wastes space, secondly it's prone to user error and two skills that are described differently but are actually the same, and thirdly because it's hard to work with for anything more complex than INSERT and DELETE operations. For example, suppose HR realises a mistake has been made: EmployeeX doesn't have any knowledge in "Skill 2", it's "Skill 5" he knows. Now write the SQL to fix that ... and bear in mind that you might have "Skill 2", and "Skill 21", "Skill 22", "Skill 23", ... so you can't rely on string replace functions or you will cause further errors. Oh, and while you are at it, fix the spelling mistake that Deirdre always makes in "Skill 17" but no one else does.

                          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                          W Offline
                          W Offline
                          Wiep Corbier
                          wrote on last edited by
                          #32

                          I'm a senior. This is not a problem for me. It works 100% perfect. I have solved this years ago. Trust me. Anyway, back to the topic please. But you already gave me an answer. Thanks. :thumbsup:

                          OriginalGriffO D 2 Replies Last reply
                          0
                          • W Wiep Corbier

                            I'm a senior. This is not a problem for me. It works 100% perfect. I have solved this years ago. Trust me. Anyway, back to the topic please. But you already gave me an answer. Thanks. :thumbsup:

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

                            Then I feel sorry for the company that employs you - that is a rookie mistake, and generally means the rest of the DB design is as full of errors ...

                            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                            "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

                            W 1 Reply Last reply
                            0
                            • OriginalGriffO OriginalGriff

                              Then I feel sorry for the company that employs you - that is a rookie mistake, and generally means the rest of the DB design is as full of errors ...

                              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

                              W Offline
                              W Offline
                              Wiep Corbier
                              wrote on last edited by
                              #34

                              Sure.

                              1 Reply Last reply
                              0
                              • L Lost User

                                Wiep Corbier wrote:

                                It has to do with the fact you call it dumb.

                                I don't call it dumb, it is dumb. And the argument re petrol or electric cars is not even remotely connected. Electric cars are all easily identified as different from their petrol counterparts.

                                Wiep Corbier wrote:

                                don't waste your time on me.

                                At last a sensible suggestion.

                                K Offline
                                K Offline
                                kalberts
                                wrote on last edited by
                                #35

                                Ten years from now, kids will honestly believe that Elon Musk invented the electric car, and all later (and earlier) electric cars are derived from the Tesla series.

                                L 1 Reply Last reply
                                0
                                • W Wiep Corbier

                                  The point is Richard, I understand the problems. But imagine this. You work in a office room with a colleguea also called Richard. A person walkes in and asks for Richard. People will ask, which Richard? (popup intelli) Got it? I know it doesn't work yet, and I know why. I just want it to work in the future. ps. C# (8?) now has the ability to work around null values in classes. Who asked for that? I didn't. Who cares? No one. But it's there anyway. Just....progress :)

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #36

                                  Wiep Corbier wrote:

                                  A person walkes in and asks for Richard. People will ask, which Richard? (popup intelli)

                                  OK, how is the compiler going to "popup a dialog" to ask which version of the class you want when you try to "new it up"?

                                  CandidateFunction newCandidate = new CandidateFunction()
                                  

                                  WHICH "CandidateFunction" does the code "new up"? How is it going to know? There's no way for the compiler or the code to generate a prompt to ask. Even if it was possible, the UI type of the app comes into play. Is this a Console app? Windows Forms? WPF? ASP.NET? Each handles prompting the user is different ways, so how is the compiler to know which to use? Oh, and how about apps with no user interface at all, like Window Services and WebAPI? How is that prompt supposed to show up? This would have to be prompted for at compile-time. There's absolutely no way to do this at run-time. On top of that, how is the compiler to know which type of object it is when you go to use that object elsewhere? Did you really think about this before posting? If you want this, then it's your skill set that needs to be improved, not the compiler.

                                  Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                  Dave Kreskowiak

                                  1 Reply Last reply
                                  0
                                  • W Wiep Corbier

                                    Like electric cars, dumb idea, petrol heads said. Because, they don't know better.

                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #37

                                    Invalid comparison. That's not even close to what your original post is about. You're thinking of the problem you posted about in terms of people, not in terms of a computer. When building your car, which engine is the car supposed to get, gas, diesel, or electric, if they are a class called Engine? How is the computer supposed to know? Change your mindset.

                                    Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                    Dave Kreskowiak

                                    W 1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      Invalid comparison. That's not even close to what your original post is about. You're thinking of the problem you posted about in terms of people, not in terms of a computer. When building your car, which engine is the car supposed to get, gas, diesel, or electric, if they are a class called Engine? How is the computer supposed to know? Change your mindset.

                                      Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                      Dave Kreskowiak

                                      W Offline
                                      W Offline
                                      Wiep Corbier
                                      wrote on last edited by
                                      #38

                                      Asking questions is a skill I will never ask a question on this platform again. Thanks for your reply. Have a nice day.

                                      D Z 2 Replies Last reply
                                      0
                                      • W Wiep Corbier

                                        Hi Pete, All these problems are already solved by me. Again. I store Skills as a string in the database. They are stored like this: Skill 1|Skill 2|Skill 3|Skill 4 of course the data is different but I show you the idea Then, customers (being developers) asked me if i couldn't provide a class with data stored in Skills as a list A list Skills has "items"

                                        public class Skill
                                        {
                                            public string Item { get; set; }
                                        }
                                        

                                        It doesn't matter if there is data in it, I handle that. So, I said to my customers I could do what the want but they need to address another Class:

                                        CandidateFunctionWithSkillsList

                                        No problem. But it made me think: what if my customers could make a choise how to recieve the data using the same name for the claas but had the option how it was presented/formatted. It is just something I would like. So I went on searching and discovered there were alternatives. But not exactly what I want. It doesn't exist. So, my question to the C# team is: can you make it happen in a next release.

                                        P Offline
                                        P Offline
                                        Pete OHanlon
                                        wrote on last edited by
                                        #39

                                        Wiep Corbier wrote:

                                        All these problems are already solved by me.

                                        Errr, no you haven't. You have talked about one narrow point and extrapolated that to having answered my whole question. I'll put this to you another way. Describe EXACTLY how this mechanism would work. How would it work with vars? How would it run through a CI pipeline? How will it cope with type explosions and types with differing numbers of fields? I want this class to be COM Compatible, how's that going to work? I want to use IoC to inject one of these classes, which one would it pick? In other words, explain how you plan to cope with ALL of the edge cases that would be introduced.

                                        Advanced TypeScript Programming Projects

                                        1 Reply Last reply
                                        0
                                        • W Wiep Corbier

                                          I'm a senior. This is not a problem for me. It works 100% perfect. I have solved this years ago. Trust me. Anyway, back to the topic please. But you already gave me an answer. Thanks. :thumbsup:

                                          D Offline
                                          D Offline
                                          Dave Kreskowiak
                                          wrote on last edited by
                                          #40

                                          Just because "it's worked for years", doesn't mean it was designed properly. The list of skills NEVER should have been a string holding multiple values like that. It should have been a separate table with foreign keys back to the people that have those skills. The skill is stored ONCE and can be used multiple times. This saves space in the database. It's not the language that needs to updated to support your poor skills. It's your skills that need to be updated to better support your customers.

                                          Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
                                          Dave Kreskowiak

                                          W OriginalGriffO 2 Replies 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