Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having separate copy of base class static member in each derived class

I have following class structure:

public abstract class PresenterBase
{
    public static Dictionary<string, MethodInfo> methodsList;

    public void Bind()
    public void Initialize();
}


public class DevicePresenter: PresenterBase
{
   public void ShowScreen();
   public void HandleEvents();    
}

public class HomePresenter: PresenterBase
{
   public void ShowScreen();
   public void HandleEvents();
}

I want to have HomePresenter and DevicePresenter to have separate copy of methodsList static member defined in PresenterBase.

Unfortunately they share the same copy with above implementation.

Is they alternative approach, that I can have separate copy of methodsList for HomePresenter and DevicePresenter? I am not willing to define methodsList in derived classes because in future if someone adds another derived class he will have to keep in mind to add methodsList to that class.

like image 245
Tushar Kesare Avatar asked Dec 27 '22 23:12

Tushar Kesare


1 Answers

Don't make it static at all. Won't that work?

static means associated with the type; non-static means associated with the instance.

I don't have a Visual Studio instance handy, but I believe you could also mark the field abstract in the base class; then the compiler will require you to add it to any deriving classes. You can definitely do that with a property.

On another note, given the above code, I would probably add abstract methods ShowScreen() and HandleEvents() to PresenterBase.

like image 196
user Avatar answered Dec 30 '22 12:12

user