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. Other Discussions
  3. The Weird and The Wonderful
  4. 'Item': member names cannot be the same as their enclosing type

'Item': member names cannot be the same as their enclosing type

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharphelp
29 Posts 8 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.
  • W WilliamSauron

    PIEBALDconsult wrote:

    True, but it would still likely generate the same name, for convenience

    Yes, and since in some assembler languages (the one that comes to mind is Compass but there are others,) labels beginning with a dot must be local, that would make sure your constructors cannot be called from outside your module.

    PIEBALDconsult wrote:

    My copies of the MS and ECMA specs say the same thing... it's an identifier, not a name.

    Yes, if you prefer calling that an identifier instead of a name (let's not reach for the scissors and talk about the subtle semantic differences between a name and an identifier.) But still a method identifier, not a type identifier. Last time I looked, the identifier denoting a constructor in my Visual Studio editor had the same black color as another method identifier, not the greenish color of my class identifier. Are they all color blind at Microsoft?

    -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #20

    WilliamSauron wrote:

    Visual Studio editor had the same black color

    Tools|Options|Environment|Fonts and Colors|Text Editor|Identifier VS doesn't discriminate between these identifiers, but I could write an editor that does. (I'll stick with Edit.) P.S. I finally got around to installing Leppie's xacc.ide -- it displays constructor identifiers in the greenish color.

    modified on Friday, November 28, 2008 1:07 PM

    1 Reply Last reply
    0
    • R Redwan Albougha

      My schoolmate told me about a situation he faced while coding using C#. if we have the following code snippet :

      public class Item
      {
      public int this[int y]
      {
      get
      {
      return 0;
      }
      }
      }

      While compiling an error will appear saying: 'Item': member names cannot be the same as their enclosing type :wtf: First I got confused, since I knew that I was defining indexer property for the class 'Item'. After I referred to MSDN I read this : Compiler Error CS0542 If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. At the end I think that this MSIL specification mustn't be exist at all.:cool:

      Best wishes, Redwan Al-Bougha

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #21

      Try this:

      public class Item
      {
      [IndexerName("IRDUM")]
      public int this[int y]
      {
      get
      {
      return 0;
      }
      }
      }

      xacc.ide - now with TabsToSpaces support
      IronScheme - 1.0 beta 1 - out now!
      ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

      R 1 Reply Last reply
      0
      • W WilliamSauron

        Ok, it's time for a little nitpicking then :-) (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game :-) It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something. The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class. This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity :-) On the other hand, now we know where the coding horror is :-)

        -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #22

        WilliamSauron wrote:

        This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers?

        No, they provide an easy way around it. See IndexerNameAttribute.

        xacc.ide - now with TabsToSpaces support
        IronScheme - 1.0 beta 1 - out now!
        ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

        W 1 Reply Last reply
        0
        • W WilliamSauron

          Ok, it's time for a little nitpicking then :-) (or, as we say in French, cutting hairs in four). Beware, I've 44 years of experience at that game :-) It means that everything I say here has absolutely no importance at all, and is by no way an attack on anyone. It's just saying something for the pleasure of saying something. The ".ctor" is not a C# name, it is a CLR/MSIL/whatever name. When I write a constructor for my C# class, I dont type ".ctor", I type the name of my class as the name of the method. Now, whether the compiler has an urgent need to create MSIL code named ".ctor" or "Groborozgruduruk" is totally irrelevant. When I program in C#, and also when I read C# code written by anyone else, I know that if a method is declared that has the same name as the class, it is a constructor. In Delphi, type casts use the same syntax as a function call, so having a function and a type with the same name is not a good idea there neither. Other languages may still have other good reasons to abhor that. This is a good reason why I agree with the compiler when it insists that I don't call my method the same name as the class. This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers? Of course, it means that you cannot have a class named "Item" that has an indexer. But wait! Do you really think a good design would involve a class named very generically "Item" that has a default indexer (and so is itself composed of a collection of something else.) What are these called then? "SubItem" maybe? What if SubItem has itself an indexer? "SubSubItem"? "YetAnoterSubItem"? If you don't give meaningful names to your classes and methods, you are of course welcome to do so, but please don't say the compiler is restricting your creativity :-) On the other hand, now we know where the coding horror is :-)

          -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

          R Offline
          R Offline
          Redwan Albougha
          wrote on last edited by
          #23

          First of all thanks for your reply it was really interesting to read it, especially:

          WilliamSauron wrote:

          but please don't say the compiler is restricting your creativity

          WilliamSauron wrote:

          On the other hand, now we know where the coding horror is

          I have a lot to say, but Midterm is next week so I can't write a lot now. BYE ;)

          Best wishes, Redwan Al-Bougha

          1 Reply Last reply
          0
          • L leppie

            Try this:

            public class Item
            {
            [IndexerName("IRDUM")]
            public int this[int y]
            {
            get
            {
            return 0;
            }
            }
            }

            xacc.ide - now with TabsToSpaces support
            IronScheme - 1.0 beta 1 - out now!
            ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

            R Offline
            R Offline
            Redwan Albougha
            wrote on last edited by
            #24

            Thanks leppie, Rob Graham already told me about IndexerNameAttribute. But thanks for your interest :cool:

            Best wishes, Redwan Al-Bougha

            1 Reply Last reply
            0
            • L leppie

              WilliamSauron wrote:

              This brings the second question: as the compiler internally creates a method named "Item" as the implementation of the default indexer, does it really impose an unbearable burden on the creativity of programmers?

              No, they provide an easy way around it. See IndexerNameAttribute.

              xacc.ide - now with TabsToSpaces support
              IronScheme - 1.0 beta 1 - out now!
              ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

              W Offline
              W Offline
              WilliamSauron
              wrote on last edited by
              #25

              There is no need to have a way around it through an attribute. If a class has an indexer, then it is a collection of some other things; "Item" is therefore not a good name for that class. It is a Coding Horror. Give the class an appropriate name, and you don't need the attribute...

              -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

              L 1 Reply Last reply
              0
              • W WilliamSauron

                There is no need to have a way around it through an attribute. If a class has an indexer, then it is a collection of some other things; "Item" is therefore not a good name for that class. It is a Coding Horror. Give the class an appropriate name, and you don't need the attribute...

                -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #26

                WilliamSauron wrote:

                "Item" is therefore not a good name for that class.

                No, it's simply your opinion. :)

                xacc.ide - now with TabsToSpaces support
                IronScheme - 1.0 beta 1 - out now!
                ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                W 1 Reply Last reply
                0
                • L leppie

                  WilliamSauron wrote:

                  "Item" is therefore not a good name for that class.

                  No, it's simply your opinion. :)

                  xacc.ide - now with TabsToSpaces support
                  IronScheme - 1.0 beta 1 - out now!
                  ((lambda (x) `((lambda (x) ,x) ',x)) '`((lambda (x) ,x) ',x))

                  W Offline
                  W Offline
                  WilliamSauron
                  wrote on last edited by
                  #27

                  Okay, your mission, should you choose to accept it, is to provide one single example where "Item" is an appropriate name for a collection class :-) If you get caught or killed, the moderator will disavow any knowledge of this thread (which will self destruct in five seconds anyway)

                  -- Quidquid latine dictum sit, altum sonatur. http://streambolics.flimbase.com S. L.

                  1 Reply Last reply
                  0
                  • R Redwan Albougha

                    My schoolmate told me about a situation he faced while coding using C#. if we have the following code snippet :

                    public class Item
                    {
                    public int this[int y]
                    {
                    get
                    {
                    return 0;
                    }
                    }
                    }

                    While compiling an error will appear saying: 'Item': member names cannot be the same as their enclosing type :wtf: First I got confused, since I knew that I was defining indexer property for the class 'Item'. After I referred to MSDN I read this : Compiler Error CS0542 If your class is named 'Item' and has an indexer declared as this, you may get this error. A default indexer is given the name 'Item' in the emitted code, creating the conflict. At the end I think that this MSIL specification mustn't be exist at all.:cool:

                    Best wishes, Redwan Al-Bougha

                    C Offline
                    C Offline
                    Camilo Sanchez
                    wrote on last edited by
                    #28

                    That's right, imagine this:

                    class Dog
                    {
                    public string Dog //error: you can't do this
                    {
                    get
                    {
                    return "Fido";
                    }
                    }
                    }

                    The only member names that can be the same as their enclosing type are constructors. In your case the indexer, named Item, is named like its enclosing type and is not a constructor, so, that's where your problem is coming from. To fix this, change The name of the class

                    R 1 Reply Last reply
                    0
                    • C Camilo Sanchez

                      That's right, imagine this:

                      class Dog
                      {
                      public string Dog //error: you can't do this
                      {
                      get
                      {
                      return "Fido";
                      }
                      }
                      }

                      The only member names that can be the same as their enclosing type are constructors. In your case the indexer, named Item, is named like its enclosing type and is not a constructor, so, that's where your problem is coming from. To fix this, change The name of the class

                      R Offline
                      R Offline
                      Redwan Albougha
                      wrote on last edited by
                      #29

                      Thanks Camilo, I already know that, but I post this since it has some taste of horror. :rolleyes:

                      Best wishes, Redwan Al-Bougha

                      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