Share via


Inheritance, static variables

Question

Tuesday, January 30, 2007 9:43 AM

Let's say that I am creating a program to make catalogue pages. I derived Class LeftCataloguePage and RightCataloguePage from the base class CataloguePage. As I am using many instances of each class with many member variables for catalogue dimensions, number of cells, margin spacing, etc, it would take a lot of time to call all the member functions of each class to change the dimensions so I decided to define static variables in the base class so that the inherited classes will have their own static variables and I will be able to specify static dimensions for LeftCataloguePage and RightCataloguePage seperately, and this will change all the dimensions for all instances of the classes. The problem is that the static variables in each derived class are not seperate, when you change the static variable of one inherited class it changes the value of the static variables in every inherited class as well as the base class. Is there anyway to inherit static variables seperately for each class so if I change a static variable in one derived class it doesnt change the same static variable in another inherited class. I have heard polymorphism isn't applicable to static variables and I find that rewriting the exact same class  again as opposed to derivation is just tedious.

Thanks.

Eitan

All replies (5)

Tuesday, January 30, 2007 1:21 PM ✅Answered

Why would you make the variable static? The following code should work just fine.

class MyBase
{
    private int myProp = 0;

    public virtual int MyProp
    {
        get { return myProp; }
        set { myProp = value; }
    }
}

class MyFirstDerived : MyBase {}

class MySecondDerived : MyBase {}

If you make the private myProp variable static, you will notice the behavior you see - that is precisely what static is intended for.


Tuesday, January 30, 2007 1:56 PM ✅Answered

Ah, I misunderstood
I that case, you should have the static variable in your derived instance:

class MyFirstDerived : MyBase
{
    private static int myVal = 0;
    public override int MyProp
    {
        get
        {
            return myVal;
        }
        set
        {
            myVal = value;
        }
    }
}

The static variable will be shared across all instances of the type.


Tuesday, January 30, 2007 9:50 AM

If you want separate values to be set for each class, why not just make the values non-static and protected, and set them to the values that you want in the ctor? Then you wouldn't need to do anything extra in the code that actually calls 'new' to set the values, and you could have different values for each type.


Tuesday, January 30, 2007 10:25 AM

I would like the user to be able to provide the dimensions during run time after the initialization of the object. I would also like the values to be passed to two different forms and the only way I know how to do that besides passing the objects to the second forms constructor (I don't want to do it this way) is by using static variables.

Thanks.

Eitan


Tuesday, January 30, 2007 1:47 PM

Let say that I had many instances of the same class in my program. I would like to be able to change all the member variables of all instances of a class by changing the member variable of one instance of a class during run time. So lets say in the catalogue program I have many instances of the CataloguePage class (a catalogue has many pages). I want the user to be able to change the page dimensions during run time. I would hate to have to access every instance in my program and change all the properties as opposed to just changing the static variables of the class itself during run time. I hope this makes sense.

Eitan