Implementing interface & redefining a method.

Morning,

public interface MyInterface{
	int Calculate();
}
public class MyClass:MyInterface{
	int Calculate(){return 0;}
	int Calculate(){return 1;}
}

Open in new window


Obviously the above code is very wrong as Calculate is defined twice within MyClass, but is there a way I can state that one of the Calculate methods only be accessible through MyInterface and the other Calculate method available through MyClass. So:

void Test(){
	
	MyClass a=new MyClass();
	a.Calculate();	//Returns 0

	MyInterface b=(MyInterface)a;
	b.Calculate();	//Returns 1
}

Open in new window


Any tricks to make this possible without having two different named methods?

Thank you,
Uni
LVL 3
Unimatrix_001Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
You could force one to require a parameter so they would still have the same name but be different.

Having said that what you are wanting will lead to really difficult to read (and debug) code
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Unimatrix_001Author Commented:
Oh of course!  :)And using the new (well, perhaps not so new now) default arguments...

public interface MyInterface{
	int Calculate(bool aIgnore=true);
}

public class MyClass:MyInterface{

	int Calculate(){
		return 0;
	}

	int Calculate(bool aIgnore=true){
		return 1;
	}

}

void Test(){
	
	MyClass a=new MyClass();
	a.Calculate();	//Returns 0

	MyInterface b=(MyInterface)a;
	b.Calculate();	//Returns 1
}

Open in new window


That should suffice, yes?

Thanks,
Uni
0
Unimatrix_001Author Commented:
Excellent, thank you very much. :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.