locked
"Member names cannot be the same as their enclosing type." RRS feed

  • Question

  • Normally if I use a field whose name is also the name of a class, then this error -- "Member names cannot be the same as their enclosing type." -- is thrown. However, when using the PhysX.Net bindings (x64), I can do this.

    using PhysX;
    
    public class Example
    {
    
        public Example Example { get; set; } // throws error
        public Scene Scene { get; set; } // uses PhysX, does not throw error
    
    }

    Thursday, September 3, 2015 5:44 PM

Answers

  • Hi Tristan,

    the member-name cannot be the class-name that contains that member. But the member-name can be the same as the return-type (what you have with the Scene).

    So this does not work:

    public class Example
    {
        public Example Example { get; set; } // throws error
    }

    The reason is that the member "Example" is already reserved for the constructor. So you can't define any other member with that name.

    But what works is the following:

      public class Example
      {
       
      }
      public class AnotherClass
      {
        public Example Example { get; set; }
      }

    Now the Example-Property is in a class called "AnotherClass". So the member-name is different from the type that contains it. Works fine.

    And when you look now at your working "Scene"-property, this only works because it's not in a class called "Scene", but in a class called "Example". It has nothing to do with PhysX. :-)


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-courses:
    XAML Layout in Depth
    Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com

    Thursday, September 3, 2015 6:14 PM
  • Example is a property of type Example, in the class ("enclosing type") Example.

    This error has nothign to do with the type and the Property name being the same. For example, this would also give you the same compile time exception:

    public class Example
    {
      public string Example { get; set; }
    }

    If the property/field has the same name as the class, the compiler would be unable to know if you mean the field/property or the class in any given context.

    //This could be the start of either accessing some static method, or the property. Wich of those two is it?
    Example

    Thursday, September 3, 2015 9:17 PM

All replies

  • Hi Tristan,

    the member-name cannot be the class-name that contains that member. But the member-name can be the same as the return-type (what you have with the Scene).

    So this does not work:

    public class Example
    {
        public Example Example { get; set; } // throws error
    }

    The reason is that the member "Example" is already reserved for the constructor. So you can't define any other member with that name.

    But what works is the following:

      public class Example
      {
       
      }
      public class AnotherClass
      {
        public Example Example { get; set; }
      }

    Now the Example-Property is in a class called "AnotherClass". So the member-name is different from the type that contains it. Works fine.

    And when you look now at your working "Scene"-property, this only works because it's not in a class called "Scene", but in a class called "Example". It has nothing to do with PhysX. :-)


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-courses:
    XAML Layout in Depth
    Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com

    Thursday, September 3, 2015 6:14 PM
  • Your class name is Example and your Property Type is Example and your variable name is Example as well. This will throw a compile error. You should consider renaming your variable name such as:

    public class Example
    {
    
        public Example MyExample { get; set; } // throws error
        public Scene Scene { get; set; } // uses PhysX, does not throw error
    
    }

    If you try this same property for the Scene type in the Scene class, it will give an error as well.

    Thursday, September 3, 2015 7:47 PM
  • Example is a property of type Example, in the class ("enclosing type") Example.

    This error has nothign to do with the type and the Property name being the same. For example, this would also give you the same compile time exception:

    public class Example
    {
      public string Example { get; set; }
    }

    If the property/field has the same name as the class, the compiler would be unable to know if you mean the field/property or the class in any given context.

    //This could be the start of either accessing some static method, or the property. Wich of those two is it?
    Example

    Thursday, September 3, 2015 9:17 PM