Resolve "Member Name Cannot Be the Same as Enclosing Type" Error

Today one of my colleagues was working to create a new project to learn something. He is a novice in C# and knows little about the C# programming and the basic structure of naming the file names.
 
He supposedly named the file as a generic name; i.e., he named it Profile.aspx and continued to work. At some point he later on added the features and some data-oriented architecture to streamline his work and then tried to build the project but the project was not building up and he discussed this with some guys near his workstation for guidance but they couldn't help.
 
So I jumped in,  and later on after checking the title of the error I thought, it is the same nature of error I have faced several times during my work, so I have diagnosed and fixed it. I want to share the full story of how I did it and I have come up with two cases to check and resolve the error.
 
So let's move forward to firstly create the scenario of the error and then later on to resolve it.
 
To do our task we need to create a new project.
 
Open the Visual Studio instance by Pressing Windows Key + R and typing devenv.exe  and presing the OK button, it opens up the new instance.
 
 
 
Create New Website by File > New > Website or Press Shift + Alt + N.
 
  
 
Let's Create a New Page (Press Ctrl + Shift + A), as I thought to create the project having the functionality of signup, login and user profile. so I created pages of the functionalities I required and I named it Signup.aspx, Login.aspx and Profile.aspx to do my task.
 
Before proceeding, as previously discussed we have two cases for this issue.
 
Case 1 (Page)
 
In case one we have named the Page profile.aspx and as you are aware that  Profile is a built-in Type which originates from System.Web.Profile.

Let's build the solution.  What happened now? Oh Ho! It throws an exception. 
 
So in this case if you name the page with this type which is built in to  C# it throws exception of type  "member name cannot be same as enclosing type".
 
Check the image for better reference.
 
 
As you can see that Error Description elaborates that 'Profile' member names cannot be the same as their enclosing type, the first enclosed character is the type which is same as the enclosing type,  which is Profile also. 
 
According to MSDN "The members of a class or struct cannot have the same name as the class or struct, unless the member is a constructor.
 
The syntax of the error is 'user-defined type' : member names cannot be the same as their enclosing type 
 
Resolution

You need to rename the file to UserProfile.aspx and then build and check it.
 
 
 
Woah, the build succeeded and it resolved the issue. Let's move on to the second case. 
 
Case 2
 
The second case lies in the method used in the page. After renaming or adding a new page to the solution case 1 is resolved. Now in the second case we want to write some method under the inherited class, but this time we also use the same name as  the page name like this. 
  1. public partial class UserProfile : System.Web.UI.Page  
  2. {  
  3.     protected void Page_Load(object sender, EventArgs e)  
  4.     {  
  5.   
  6.     }  
  7.   
  8.     public void UserProfile()  
  9.     {  
  10.         Response.Write("New Method Added");  
  11.     }  
  12. }   
Let's see what happened.
 
 
 
As you can see it throws the same error.
 
This time what happened is you named a method the same as your page name, so as per the law under class, the member names are not the same as their enclosing types; in our case, the UserProfile is the enclosing type.
 
Resolution
 
To resolve this error we should rename it to something different from our class name so let's do that --  I have renamed it to GetUserProfile and see if this can this resolve our error.
 
Build the solution and check the result.
 
 
Yes, we have resolved the error. That's great.
 
So today we have seen the common error and its resolution.

Now it's your turn --  if you have any cases then let me know I will add it to this resource.


Similar Articles