2
class Frame
{
    static int X;
    static int Y;
    static uint Color;

    static protected bool Check()
    {
        return (Coords.GetPixelColor(X, Y) == Color);
    }
}

class frameBeginning : Frame
{
    static int X = 1;
    static int Y = 2;
    static int Color = 3;
}

frameBeginning.Check(); cannot compile, because Check() is inaccessible due to its protection level.

But why, Check() is protected?

moinudin
  • 134,091
  • 45
  • 190
  • 216
John black
  • 79
  • 3
  • siz is right. btw, you should use a coding standard. Generally, classes start with a capital letter. If you don't like that, at least be consistent everywhere (Frame vs frameBeginning). – dontangg Jan 03 '11 at 15:33

6 Answers6

8

This is not a problem. The behavior you are seeing is part of the definition of protected. If you want to call frameBeginning.Check from outside the code of the frameBeginning or frame class, the method needs to be public or internal.

Szymon Rozga
  • 17,971
  • 7
  • 53
  • 66
4

Because you declared it as protected in the code shown above.

But different rules apply to static methods. Check out the SO question below:

C#: Can a base class use a derived class's variable in a static function?

Community
  • 1
  • 1
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
2

You could call Check from within frameBeginning, but not outside the class. This is what protected means: visible to derived classes, not to external code.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
0

Using frameBeginning.Check() from outside needs that Check() is public. Being protected is only visible to derived clasess, but not from outside. You're trying to access the Check() method from outside, so you need to do Check() public or internal.

In other words, you could acess Check() from a frameBeginning instance, but Check() is static, if you want to access Check() from outside, you need to change the visibility to public (or internal).

Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
0

So that the class itself and all inherited classes can use the method, independent of an instance of the object, but anything outside of the class can not use the method.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
0

There are 5 access modifiers... you need to understand what kind of access you can get by each.

private - Function can only be called from within the class

protected - Function can be called only from within the class and all its derived classes

internal protected - Function can be called by the class and all its derived classes inside the Assembly

internal - Almost like public - Can be called from anywhere inside the Assembly ... Used for Incapsulation

public - Can be called from anywhere

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185