Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define virtual method with return type which is not void in C#

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.

like image 486
Lamar Avatar asked Feb 12 '23 17:02

Lamar


2 Answers

You can throw NotImplementedException instead of returning object:

    public virtual object Method()
    {
        throw new NotImplementedException();
    }

But if you are not implementing anything in virtual method you can create abstract instead of virtual:

    public abstract object Method();

Edit:

Another option is to create interface for it.

public interface IMethods
{
    object Method();
}

And make your classes children of this interface.

like image 145
Vano Maisuradze Avatar answered Feb 15 '23 10:02

Vano Maisuradze


you need to use abstract here. The abstract modifier indicates that the thing being modified has a missing or incomplete implementation.

public abstract returntype MethodName();

But as you say, 'since this method is fully dependent on inherited class, therefore all code will be in the override methods', than if you are really going to override the functionality of the method in inherited class, why do you care if the method returns dummy or stuff? (e.g: you can make it virtual and get going)

Edit: as you cannot mark class as abstract, you can use virtual method instead.

public virtual returntype MethodName()
{
   .....
   return xyz;
}

(just for info: An abstract member is implicitly virtual. and abstract is sort of pure virtual. so you need virtual, instead of pure virtual)

like image 42
Zeeshan Avatar answered Feb 15 '23 11:02

Zeeshan