static and base classes

  • Thread starter Thread starter rodchar
  • Start date Start date
R

rodchar

hey all,
what are some different ways around the following violation:

i can't access a variable inside the base class from inside a static method
of the derived class

thanks,
rodchar
 
hey all,
what are some different ways around the following violation:

i can't access a variable inside the base class from inside a static
method of the derived class

Could you show a small code sample of what you are trying to do.

Unless the variable inside the base class is static as well this makes no
sense. If it is static then it must be protected, internal or public to
allow access.

Ken
 
Ken Foskey said:
Could you show a small code sample of what you are trying to do.

Unless the variable inside the base class is static as well this makes no
sense. If it is static then it must be protected, internal or public to
allow access.

I'd guess it is:

class Base { protected int i; }
class Derived { public static int Inspect(Base b) { return b.i; } }

No, that won't work because it breaks encapsulation (and security).
Protected access only grants you access through a reference known to be your
(derived) type. That could be a variable of type Derived or any further
derived type.
 
Back
Top