Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I have a base class where each derived class has its own copy of a static property?

I have something like the following situation below:

class Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base
{
}

class DerivedB : Base
{
}

I am trying to set this up so that each derived class has its own static instance of x, if I do something like this:

 DerivedA.x = 5;
 DerivedB.x = 10;

then when I run:

 DerivedA.myMethod(); //The result will be 10
 DerivedB.myMethod(); //The reusult will be 15

Can i do something like this? How can I setup the derived classes to achieve this? Thanks guys.

EDIT: Basically, I have a bunch of derived classes that each have a property unique to that class. It does not vary for each instance, and thus I believe should be a static variable. Also, that property is set by a method, that is the same for each of these classes. I am trying to avoid having to copy and paste the logic for that property and method in each of these derived classes. I thought it best to somehow move that logic to the base class from which each of these classes are derived from. But, I need each derived class to have its own copy of that property. I do not necessarily have to do it this way, and I will be happy to hear some better practices suggestions if you guys have any.. Thanks!

like image 506
ntsue Avatar asked Feb 20 '11 21:02

ntsue


2 Answers

Well, yes, you can, but it revolves around a bit of a trick with generics.

It is much better if you fix your design so that you don't need that static field, or at least not per descendant, but here goes:

class Base<TDescendant>
    where TDescendant : Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base<DerivedA>
{
}

class DerivedB : Base<DerivedB>
{
}

This relies on the fact that a generic type with static fields will get a separate copy of those static fields for each type you invoke it with.

However, if you intend to descend from DerivedA or DerivedB, it gets tricky, so I wouldn't recommend going down this lane.

like image 109
Lasse V. Karlsen Avatar answered Sep 19 '22 05:09

Lasse V. Karlsen


You will need to redefine and hide the field and method in all derived types.

Example:

class DerivedA : Base
{
  public new static int x;
  public new int myMethod()
  {
    x += 5;
    return x;
  }
}

Note: don't do it this way. Fix your design.

Edit:

Actually, I have a similar construct. I solve it with an abstract (if you need a default value, use virtual) property which then gets used from the base class:

public abstract class Base
{
   public abstract string Name { get; }

   public void Refresh()
   {
     //do something with Name
   }
}

public class DerivedA
{
  public override string Name { get { return "Overview"; } }
}

You should be able to adjust that for your use case. You can of course make the property protected if only deriving classes should be able to see it.

like image 39
Femaref Avatar answered Sep 20 '22 05:09

Femaref