views:

50

answers:

3

How do I get around Inconsistant accessibilty error in C # ?

I need to pass a pointer to a node in a linked list to a method. When I do, I get a "Compiler Error CS0051"

Example The following sample generates CS0051:

// CS0051.cs
public class A
{
  // Try making B public since F is public
  // B is implicitly private here
  class B
  {
  }

  public static void F(B b)  // CS0051
  {
  }

  public static void Main()
  {
  }
}

That is a simple example. The actual program is a bit more complicated. I am actually using a node in a linked list to pass to the method LinkedListNode node

The method uses recursion because the node is mart of a huge linked list structure of linked lists that outputs an xml file. Either I have to find a way to use recursion without using methods or I need to find a way to pass pointers nodes or actual nodes.

+4  A: 

You cannot have a parameter defined as less than public on a public method.

In the example above, B should be public if you want to use it on the public F method.

Morten Mertner
A: 

The only way you could "get around" it is to make the method take a base type that is public as parameter instead of B. You can use object if there is no other base type. This is only useful if you have a public method that returns an internal type that later needs to be passed back to your code and it's generally not a good design. (Of course, the returning method would also have to return a public type.)

Otherwise you just have to make B a public type to make it work. What your code is saying right now is: anyone can call this method, but only classes in this assembly can access the parameter type. Of course, if they cannot access the parameter type then they cannot really call the method - that's what the C# compiler is telling you.

Evgeny
A: 

If you think about a user of class A, they will be offered a public interface which includes void F(B b) . It's only logical that this user should be allowed to instantiate a B, otherwise the method F would be unusable.

spender