private and internal


T

Tony Johansson

Hello!

If I have class Line in my program and compile I get these errors
Error 1 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.starting'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 90 20
ConsoleApplication1
Error 2 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.ending'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 91 20
ConsoleApplication1

The reason for this is because I define a class within another class the
Point class will have access rights as Private.
So private is less accessible then public which is correct.

But if I set the access right to internal on the Point class it's still less
then public but it will work.

So the error message is in some way wrong because as I mentioned having
internal on the Point is less accessible then
field 'Line.staring' and field 'Line.ending'

I'm I right in my conclusion ?

class Line
{
class Point
{
int x = 0;
int y = 0;
}

public Point starting = new Point();
public Point ending = new Point();
}

//Tony
 
Ad

Advertisements

M

Marcel Overweel

Tony Johansson said:
Hello!

If I have class Line in my program and compile I get these errors
Error 1 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.starting'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 90 20
ConsoleApplication1
Error 2 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.ending'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 91 20
ConsoleApplication1

The reason for this is because I define a class within another class the
Point class will have access rights as Private.
So private is less accessible then public which is correct.

But if I set the access right to internal on the Point class it's still
less then public but it will work.

So the error message is in some way wrong because as I mentioned having
internal on the Point is less accessible then
field 'Line.staring' and field 'Line.ending'

I'm I right in my conclusion ?

class Line
{
class Point
{
int x = 0;
int y = 0;
}

public Point starting = new Point();
public Point ending = new Point();
}

//Tony

If Point is private (as it is in the above example), Point can't be used
as a field because Point is invisible to the outside world.
The outside world is everything outside class Line { ... }.

But, if Point is internal, Point IS visible to the outside world but
limited to the assembly where it is defined. For the same assemby,
Point is considered being public while outside the assembly, Point
is private.

I concur with you that the message is a bit misleading.
Internal is also less accessible than public.
But in this case, going private is a programming error,
going internal is not.

regards,
Marcel
 
A

Abubakar

So private is less accessible then public which is correct.

which "public"? Both your classes are private (Line & Point).
 
H

Harlan Messinger

Tony said:
Hello!

If I have class Line in my program and compile I get these errors
Error 1 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.starting'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 90 20
ConsoleApplication1
Error 2 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.ending'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 91 20
ConsoleApplication1

The reason for this is because I define a class within another class the
Point class will have access rights as Private.
So private is less accessible then public which is correct.

But if I set the access right to internal on the Point class it's still less
then public but it will work.

So the error message is in some way wrong because as I mentioned having
internal on the Point is less accessible then
field 'Line.staring' and field 'Line.ending'

I'm I right in my conclusion ?

class Line
{
class Point
{
int x = 0;
int y = 0;
}

public Point starting = new Point();
public Point ending = new Point();
}

In these messages, "accessible" doesn't refer to the access modifiers,
in the sense that the access modifiers can be arranged in a sequence
from greatest to least accessibility as {public}, {protected, internal},
{private}, but to the items. It means that there are places where
starting and ending are visible but Point, the type of their return
value, isn't. That is, Point, is less accessible than either starting or
ending.

You are correct that if you make Point internal, the error messages ago
away. When you do that, the set of possible clients of Line that can see
starting and ending but that can't see Point becomes the empty set. To
those clients, Point is now no less accessible than starting and ending.

But then see what happens when you make Line public. By doing that, you
will have made Line available to new clients (that is, the universe of
classes that are outside the assembly) that can see starting and ending
but that can't see Point. In that situation, Point will once again be
less accessible than starting and ending, and the error messages will
return.
 
Ad

Advertisements

B

Ben Voigt [C++ MVP]

Tony Johansson said:
Hello!

If I have class Line in my program and compile I get these errors
Error 1 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.starting'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 90 20
ConsoleApplication1
Error 2 Inconsistent accessibility: field type 'Line.Point' is less
accessible than field 'Line.ending'
F:\C#\ConsoleApplication1\ConsoleApplication1\Program.cs 91 20
ConsoleApplication1

The reason for this is because I define a class within another class the
Point class will have access rights as Private.
So private is less accessible then public which is correct.

But if I set the access right to internal on the Point class it's still
less then public but it will work.

So the error message is in some way wrong because as I mentioned having
internal on the Point is less accessible then
field 'Line.staring' and field 'Line.ending'

I'm I right in my conclusion ?

No, the fields both have "internal" accessibility because they are defined
in an "internal" class type (Line). So their type (Point) is not less
accessible than the fields using it.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top