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?
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.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"spenular" <spen...@gmail.com> wrote in message
news:1108413603.4...@z14g2000cwz.googlegroups.com...
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
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.
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<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
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/
There is no polymorhism with "overridden" static methods. Consequently, the
single impl to call is not resolved at runtime but compile time rather.