Saturday, August 09, 2008

Member names cannot be the save as their enclosing type

Error: Member names cannot be the save as their enclosing type

Possible Cause: Unkowingly I declare Method name, same as class name, which pops-up this error. Logically its out of blue to think, declaring method name as class name as it is constructor, but may be because of nature of application i have declare Class Name as Method Name and runs into Error: "Member names cannot be the save as their enclosing type".

Cause of Error:


public class MyClass
{
//Declaring Method Same as Class Name
//Note: its not constructor, constructor don't have return type.
public void MyClass()
{
}
}
 

Solution to Error:


public class MyClass
{
//Change Method Name to Something else
public void MyMethod()
{
}
}

4 comments:

Enigma said...

What if you actually want to make a constructor?

Remove the value before the name (usually void or int or string etc)

Constructors have no return types so check the syntax again.

e.g.

public class myClass
{
public myClass()
{
}

}

DotNetGuts said...

Chris,

Thanks :) I am knowing that constructor have no return type, but have you tried reading the whole description, which states, mistakenly i have declare METHOD name as CLASS name, which cause this whole confusion and I ran into error.

Unknown said...

i have a question along the same line.. i can't figure out why i get the same error in this program section.

public class Loan
{
private double loanSum, APR, bal, prin, monthsInt, totIntPaid, amountPaid;
private int numberPay;

public Loan()
{
}
public Loan(double Loans, double intRate, int yrs)
{
loanSum = Loans;
if (intRate < 1)
APR = intRate;
numberPay = 12 * yrs;
totIntPaid = 0;
}

DotNetGuts said...

@Skylar

You should not receive this error, check the whole class code, you might have declare Method Name Loan in your Loan Class.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape