How to declare a variable such that it exists once per class, kind of like static, but derived classes would also receive a new instances
I am looking for a way to declare a variable such that it exists once
per class, kind of like static, but derived classes would also receive
a new instances of the variable because they are different classes.
Example:
public class MyBaseClass
{
public static int i = 0;
}
public class MyDerivedClass : MyBaseClass
{
}
public class test
{
static void main (...)
{
MyBaseClass obj1 = new MyBaseClass();
MyDerivedClass obj2 = new MyDerivedClass();
obj2.i = 57; //Now obj1.i also equals 57 even though it is an instance of a different class (which I don't want)
}
}
In the example above I have two different objects of two
different classes, and when I set the "i" value of one, it sets the "i"
value of all. What I would like is for all instances of MyBaseClass
to share an "i" value. And all instances of MyDerivedClass to share an
"i" value. But instances of MyBaseClass do not share the "i" value
with instances of MyDerivedClass.
The only thing I can think of is to make MyBase a generic (a template class), but that complicates things elsewhere. Any ideas?
Thanks,
Jeff Plummer