Re: static methods in an interface
"Hewitt, Simon C. (Contractor)" <[email protected]>
| Newsgroups | gmane.comp.windows.devel.dotnet.cx |
|---|---|
| Message-ID | <CF362B9DFA7D2544934516DFA1459AEC074205@BP1XEUEX709-C.bp1.ad.bp.com> |
On your classes, implement the interface method(s) and just call a
corresponding static method which does the real work.
(You can make the make the instance methods explicit if you don't want
them to be public as here)
public interface IMyInterface
{
void MyStaticImplementedMethod1();
void MyStaticImplementedMethod2();
}
public class MyClass1: IMyInterface
{
private static void DoMethod1()
{
// Insert code here
}
private static void DoMethod2()
{
// Insert code here
}
public void MyStaticImplementedMethod1()
{
DoMethod1();
}
// Explicit implementation
void IMyInterface.MyStaticImplementedMethod2()
{
DoMethod2();
}
}
Cheers
Simon
-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Peter
Osucha
Sent: 24 August 2006 20:10
To: [email protected]
Subject: [DOTNET-CX] static methods in an interface
I have an interface defined with some methods. I have two classes that
implement this interface - however, I want the classes to implement the
methods statically.
I tried setting the method definition to 'static' in the interface but
apparently, that's not allowed (I get an error 'The modifier 'static' is
not
allowed for this item').
If I just try to use the 'static' modifier in the class that implements
the
interface, I get another error telling me that the class does not
implement
the interface method (which doesn't have the 'static' modifier). What
is
going on here and why am I not allowed to create a 'static' method this
way?
How might I accomplish what I am trying to do, please?
Sincerely,
Peter
===================================
This list is hosted by DevelopMentor(r) http://www.develop.com
View archives and manage your subscription(s) at
http://discuss.develop.com
===================================
This list is hosted by DevelopMentor® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com