CSM
CSM

Reputation: 33

c# alternative to abstract

How can I force derived forms to implement methods of a base class without making it abstract?

The error I receive is "The designer must create an instance of type 'X' but it cannot because the type is declared as abstract". Thank you in advance.

Upvotes: 3

Views: 1107

Answers (4)

Daniel MesSer
Daniel MesSer

Reputation: 1181

As renato said, you could create an abstract base class that requires an interface in constructor. Here's an example of when I did it in AS3, it should be easy enough to understand what happens and apply it to your solution:


public class PyramidenMain extends ProbabilityGame implements IProbabilityGame{
    public function PyramidenMain(){
        super(this);
        super.initialize();
    }   

    //implement interface
}

public interface IProbabilityGame{
        function getGame():MovieClip;
        function customInit():void;
        function customLoginSuccessful():void;
        function customLoginError(xml:XML):void;
        function customShowError(msg:String):void;
        function createDemoProtocol():IProtocol;
    }

public class ProbabilityGame {
    public function ProbabilityGame(game:IProbabilityGame) {
        if(game == null) {
            throw new Exception("unmet requirement, parameter containing an instance of IProbabilityGame");
        }
        _game = game;
    }

    public function initialize() {
        //do some logic
        _game.customInit();
    }
}

Upvotes: 2

The Original Android
The Original Android

Reputation: 6215

You may override the base class and define the abstract or virtual methods. Perhaps that is what you want. It is reality in the work environment.

Upvotes: 0

Botz3000
Botz3000

Reputation: 39650

You cannot force a class to override a base class method unless it is marked as abstract, which also requires the base class to be abstract. Have you thought about defining an interface for those methods?

Upvotes: 3

Renato Gama
Renato Gama

Reputation: 16599

I would create an interface so that every form would implement it. Could you detail a bit more your problem?

Upvotes: 12

Related Questions