Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Overriding method and returning different objects

Tags:

c#

I have a virtual method on a base class. Each time I override it, I want to return different object types. I know this is not possible, but what would be the best way to handle this situation?

Example of base method:

public virtual void clickContinue() 
{
    //click the continue button
}

And the method that overrides it:

public override myObject clickContinue()
{
    //click then continue button
    //return myObject
}

I need to do several similar overrides, all returning different objects. Again, I know this can't be done the way it's done above - I trying to figure out the best way to handle this situation.

like image 345
jc3131 Avatar asked Mar 16 '23 00:03

jc3131


1 Answers

I know this is not possible, but what would be the best way to handle this situation?

If you don't need a default implementation, you can potentially make the class generic, and return the generic type:

abstract class YourBase<T>
{
     abstract T ClickContinue();
}

class YourOverride : YourBase<MyObject>
{
     override MyObject ClickContinue()
     {
        //...
like image 195
Reed Copsey Avatar answered Apr 01 '23 00:04

Reed Copsey