views:

196

answers:

4

I have this in my base class:

protected abstract XmlDocument CreateRequestXML();

I try to override this in my derived class:

protected override XmlDocument CreateRequestXML(int somevalue)
{
    ...rest of code
}

I know this is an easy question but because I've introduced a param is that the issue? The thing is, some of the derived classes may or may not have params in its implementation.

A: 

Yes the parameter is the issue, the method signature must match exactly (that includes return value and parameters).

John Rasch
+4  A: 

When you override a method, with the exception of the word override in place of virtual or abstract, it must have the exact same signature as the original method.

What you've done here is create a new unrelated method. It is not possible to introduce a parameter and still override.

JaredPar
Ok, so then lets say you want to make sure every class the inherits this base class to be required to implement that method. But you know that in some that there will be some incoming required params to that method and some not. Then what? Do you not require params and get them via a priv field?
A: 

If some of the derived classes need paramters and some do not, you need to have two overloaded versions of the method. Then each derived class can override the one it needs.

class Foo {    
    protected abstract XmlDocument CreateRequestXML(int somevalue);
    protected abstract XmlDocument CreateRequestXML();
};

class Bar : public Foo {    
    protected override XmlDocument CreateRequestXML(int somevalue) { ... };
    protected override XmlDocument CreateRequestXML()
        { CreateRequestXML(defaultInt); }
};

This of course introduces a problem of the user calling the wrong one. If there is no acceptable default for the extra paramter, you may have to throw an exception in the derived class if the wrong version is called.

Steve Rowe
A: 

I am very curious what you are trying to accomplish with this code.

Normally you define an abstract function when a usage class knows its signature. The usage class has a "contract" with you base class.

When you want to derive from the baseclass and add additional parameters, how does the usage class know what to call on the base class anyway?

It looks like an architectual flaw....but maybe you can add some extra information to the inital topic.

Patrick Peters