How To Force Static Method Implementation

S

spenular

I'm wondering of a way to implement functionality that would force an
inherited class to implement a static method. Here is an example:

public class MyBase
{
public static void DoSomething()
{
// This method must be implemented by all inherited classes

......
}
}

public class MyClass : MyBase
{
public new static void DoSomething()
{
.....
}
}

Note that the class MyBase has a static method defined (DoSomething).
Since the method is marked as static, it can neither be marked as
virtual or abstract. What I need is to force all inherited classes to
implement the method and I need the method to be static. I've also
tried creating an interface, however, interfaces cannot contain static
method definitions. Is anyone aware of a design pattern or method that
would do something like this?
 
N

Nicholas Paldino [.NET/C# MVP]

spenular,

There is nothing in the language that allows that. The best way to do
something like this is to have some sort of singleton class factory which
will manage the singleton instances for you, where you could fetch the
implementation you want.

Then, you would just cast it to the interface that you define.

Hope this helps.
 
M

MuZZy

spenular said:
I'm wondering of a way to implement functionality that would force an
inherited class to implement a static method. Here is an example:

public class MyBase
{
public static void DoSomething()
{
// This method must be implemented by all inherited classes

......
}
}

public class MyClass : MyBase
{
public new static void DoSomething()
{
.....
}
}

Note that the class MyBase has a static method defined (DoSomething).
Since the method is marked as static, it can neither be marked as
virtual or abstract. What I need is to force all inherited classes to
implement the method and I need the method to be static. I've also
tried creating an interface, however, interfaces cannot contain static
method definitions. Is anyone aware of a design pattern or method that
would do something like this?


I guess the best you can do is to throw an exception in the MyBase.DoSomething, it will give an
excetion in all inhrited claases unless they will redefine it.

public class MyClass : MyBase
{
public new static void DoSomething()
{
throw new Exception("Has to be re-defined!");
}
}

But keep in mind, as it's not virtual, you can't call it from MyBaseInhertant as MyBase, eg:

MyBase mb = new MyBaseInheritantClass();
mb->DoSomething(); // will always throw exception


Andrey
 
T

Tor Bådshaug

My first thought is that from an OO-point of view, chances are you should
reconsider your design.
Why do you need to force subclasses to implement the static method (who is
going to use these subclasses' methods?) ?
When invoking static methods, you must link to the methods statically
(qualifying the method you call with the class name), as opposed to
overridden instance methods when the actual method being called is
determined at runtime based on the objects runtime type. So with static
methods, you are not really going to achieve polymorphism.

Consider the following code and the output produced.

Base baseInstance = new OOPOC.Base();
Base derivedInstanceA = new OOPOC.Derived();
Derived derivedInstanceB = new OOPOC.Derived();

baseInstance.DoSome();//outputs DoSomething Base
derivedInstanceA.DoSome();//outputs DoSomething Base
derivedInstanceB.DoSome();//outputs DoSomething Derived

//baseInstance.DoSomething();//Illegal, must be Base.DoSomething()
//derivedInstanceA.DoSomething();////Illegal, must be Base.DoSomething() or
Derived.DoSomething()
//derivedInstanceB.DoSomething();//Illegal, must be Base.DoSomething() or
Derived.DoSomething()

public class Base
{
public Base()
{
}
public static void DoSomething()
{
Console.WriteLine( "DoSomething Base" );
}

public void DoSome()
{
DoSomething();
}


public class Derived : Base
{
public Derived()
{
}

public static new void DoSomething()
{
Console.WriteLine( "DoSomething Derived" );
}

public void DoSome()
{
DoSomething();
}
}

if you decide that redefining static methods in subclasses is the way to go
after all, then how to enforce it. Possibilities could include :
1)Using a runtime check. If the fact that Base.DoSomething gets called is
invalid (signals that a subclass violates the "contract" of providing a
"DoSomething"-implementation which you are trying to enforce), you could
implement Base.DoSomething as follows.

public static void DoSomething()
{
throw new ApplicationException( "DoSomething should be redefined in
subclasses" );
}

This solution assumes that Base.DoSomething() should never be called.
Relying on runtime check is not ideal of course, and whether it is
acceptable in your case depends on your requirements.

2)Build a component that you use in your build script to validate/enforce
your scheme. The reflection API might be of use.

Regards,

Tor Bådshaug
tor.badshaug (at) bekk.no.
 
J

Jeff Louie

I am having trouble with this concept. Zero or more classes can derive
from a
base class. If each derived class defines an implementation of the base
class
static method then there would be one or more implementations of the
single
method in the base class. There would be no way for the runtime to
decide
which single implementation to call at runtime.

An abstract instance method and a Singleton pattern perhaps....

Regards,
Jeff
I'm wondering of a way to implement functionality that would force an
inherited class to implement a static method<
 
C

Chad Z. Hower aka Kudzu

Jeff Louie said:
base class. If each derived class defines an implementation of the base
static method then there would be one or more implementations of the
method in the base class. There would be no way for the runtime to

Static methods cannot be virtual and thus cannot be overriden in C#. Which
IMO is a real shame - coming from Delphi its possible and quite useful.



--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
 
G

Guest

..... If each derived class defines an implementation of the base
class static method then there would be one or more implementations of the
single method in the base class. There would be no way for the runtime to
decide which single implementation to call at runtime.

There is no polymorhism with "overridden" static methods. Consequently, the
single impl to call is not resolved at runtime but compile time rather.

Tor BÃ¥dshaug
tor.badshaug (at) bekk.no.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top