hungariandude
hungariandude

Reputation: 386

About partial class (...inaccessible due to its protection level)

I have a partial class, called Point, and in them, I wanted to practice by createing a JustTest method, (which is pointless, because it just writes out a line), however I get this error "`Program.Point.JustTest()' is inaccessible due to its protection level".

public partial class Point
{
    private int x;
    private int y;

    public Point (int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    partial void JustTest();
}

public partial class Point
{
    partial void JustTest()
    {
        Console.WriteLine("Should I work?");
    }

    public int setX
    {
        set
        {
            x = value;

        }
        get
        {
            return x;
        }
    }

    public int setY
    {
        set
        {
            y = value;

        }
        get
        {
            return y;
        }
    }
}

static void Main()
{
    Point p1 = new Point(20,30);        
    p1.JustTest();
}

Upvotes: 1

Views: 1346

Answers (4)

Ric
Ric

Reputation: 13248

According to documentation:

No access modifiers are allowed. Partial methods are implicitly private.

This justifies the error you are receiving regarding the method being inaccessible.

Maybe if you provided more context to what you are trying to achieve and why, more help could be provided.

partial (Method)

Upvotes: 5

jtabuloc
jtabuloc

Reputation: 2535

Partial methods are "implicitly private, and therefore they cannot be virtual." nor can be access outside of the same class and modules. If you want it to be accessible outside you must declare public method that will access the partial methods. See explaination here.

Upvotes: 1

David Arno
David Arno

Reputation: 43254

All members (methods and fields) of a class are private by default. All partial methods must be private.

So just change the line to:

public void JustTest() ...

and all will be fine.

Upvotes: 3

MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Any member within a class is private if nothing else is declared. Therefor the compiler-error is absolutely correct because you cannot access that private method JustTest within your Program-class.

Make JustTest public instead and omit the partial-definition because any partial member is implicitely private.

Upvotes: 0

Related Questions