How to define a virtual method with a return type that is not empty in C #

advertisements

This might sound like a dumb question, but I need to write a virtual method that is being overridden by inherited class. I don't need the virtual method to have any code, since this method is fully dependent on inherited class, therefore all code will be in the override methods.

However, the method has a return type that is not void. If I keep the virtual method empty it would give me an error "no all path return a value".

The only solution I came up with was to implement the virtual method with returning a dummy empty string, but I don't feel this is the best way. Is there any other way to define a virtual method with return type?

Edit:

Even most answers were correct in their own way, they did not help in my case, therefore I am adding snippets of the code which shows why I need to create instance of the base class, and why I can't use interface, or abstract:

//base class
public class Parser
{
    public virtual string GetTitle()
    {
        return "";
    }
}

//sub class
public class XYZSite : Parser
{
    public override string GetTitle()
    {
        //do something
        return title;
    }
}

// in my code I am trying to create a dynamic object
Parser siteObj = new Parser();
string site = "xyz";
switch (site)
{
    case "abc":
       feedUrl = "www.abc.com/rss";
       siteObj = new ABCSite();
       break;
    case "xyz":
       feedUrl = "www.xzy.com/rss";
       siteObj = new XYZSite();
       break;
}
//further work with siteObj, this is why I wanted to initialize it with base class,
//therefore it won't break no matter what inherited class it was
siteObj.GetTitle();

I know the way I cast Parser object to Site object doesn't seem very optimal, but this is the only way it worked for me, so Please feel free to correct any thing you find wrong in my code.

Edit (Solution)

I followed the advice of many of replies by using interface and abstract. However it only worked for me when I changed the base class to abstract along with all its methods, and inherited the base class from the interface, and then inherited the sub classes from the base class. That way only I could make sure that all classes have the same methods, which can help me generate variant object in runtime.

Public interface IParser
{
    string GetTitle();
}

Public abstract class Parser : IParser
{
    public abstract string GetTitle();
}

Public class XYZ : Parser
{
    public string GetTitle();
    {
        //actual get title code goes here
    }
}

//in my web form I declare the object as follows
IParser siteObj = null;
...
//depending on a certain condition I cast the object to specific sub class
siteObj = new XYZ();
...
//only now I can use GetTitle method regardless of type of object
siteObj.GetTitle();

I am giving the credit to CarbineCoder since he was the one who put enough effort to take me the closest to the right solution. Yet I thank everyone for the contribution.


Since other answers have discussed about abstract/virtual implementation, I am suggesting my own version.

There is a contradiction in your requirement.

  1. You want a base class which is not an abstract but it has a method which is not implemented. Don't you think this unimplemented method will make the class incomplete and end up making it an abstract one even though you haven't explicitly said so?
  2. So lets assume your class will never be an abstract class and its perfectly reasonable to have it as a normal class. Does it make sense to remove this method from the class altogether and move it to an interface?

Can you try extracting this method and put it into an interface.

interface NewInterface
{
    string NewMethod();
}

public BaseClass
{
   ...
}

public DerivedClass : BaseClass, NewInterface
{
    public string NewMethod
    {
       ...
    }
}

If you can do this, then you need not have to worry about the base class being abstract/ having NotImplemented exception, only downside is every derived class should implement this interface, but thats the point of making the base class non-abstract.

I don't see any problem in implementing Abstract BaseClass/ Interface for your approach. Both are supposed to be the solution for your problem.

//Parser siteObj = new Parser(); - Dont initialize it here,
//your are initializing it once more below
NewIterface siteObj;
string site = "xyz";
switch (site)
{
    case "abc":
       feedUrl = "www.abc.com/rss";
       siteObj = new ABCSite();
       break;
    case "xyz":
       feedUrl = "www.xzy.com/rss";
       siteObj = new XYZSite();
       break;
 }