Export (0) Print
Expand All

Private Communication Between Components in Assemblies

Visual Studio .NET 2003

At times, you might want to create components in an assembly that are exposed to other members of your assembly, but not to clients. For example, you might have an assembly that exposes Machine and Mechanism components to the user. Each of these components might in turn require Widget components, but the client application would have no need for a Widget component on its own.

A public component is accessible to all users of an assembly, including the client, but a private component is accessible only to other members of its namespace. Neither of these access modifiers allows the level of access that you require.

The solution is to use Friend (internal in C#) components. If a component is marked as Friend (internal), it can be freely accessed and instantiated by all other components in your assembly, but not by any users outside your assembly. The following example shows how to use the Friend (internal) access modifier.

' Visual Basic
Namespace Widgets
   ' This class will be accessible to other classes in the assembly, 
   ' but not to client users.
   Friend Class Widget
      ' Insert code to implement the Widget class.
   End Class
End Namespace

' This namespace contains a class that will use a class in the Widgets
' namespace.
Namespace Mechanisms
   ' This class is accessible to client users, and can itself access the 
   ' Widget class to create and contain instances of it. 
   Public Class Mechanism
      Dim myWidget as New Widgets.Widget
      ' Insert code to implement additional functionality.
   End Class
End Namespace

// C#
namespace Widgets
{
   // This class will be accessible to other classes in the assembly,
   // but not to client users.
   internal class Widget
   {
      // Insert code to implement the Widget class.
   }
}

// The Mechanisms namespace declares Mechanism class that will contain an 
// instance of the Widgets.Widget class.
namespace Mechanisms
{
   // This class is accessible to client users, and can itself access the 
   // Widget class to create and contain instances of it. 
   public class Mechanism
   {
      Widgets.Widget widget = new Widgets.Widget();
   }
}

Access Levels for Nested Classes

When assigning access levels to nested classes, there are additional considerations. For details, see Nested Classes in Components.

Accessing Members of Friend (internal) Components

Members of Friend (internal) components can be accessed according to the same access rules as members of a public component, provided that the caller has access to the class. For example, a component named Mechanism that is a member of the same assembly as Friend (internal) component Widget can call the public method Spin of Widget, but an external user cannot. In practice, all public members of a Friend (internal) class have Friend (internal) access levels. Private members are accessible only from within the class itself.

Accessing Friend (internal) Members of Public Components

You might want to make methods or properties of a public component accessible only to members of your assembly. For example, a public Mechanism class might have a method named AddWidget. Any member of the assembly could invoke this method, but it would not make sense to allow the client to invoke it. In such a case, use the Friend (internal) access modifier on the method itself, and keep the access modifier on the class public.

Note   In the example below, the method AddWidget is accessible to all members of the assembly, but is not added to the public interface and cannot be called by client applications.
' Visual Basic
Public Class Mechanism
   Friend Sub AddWidget()
      ' Insert code to implement method.
   End Sub
   ' Insert code to implement the rest of the class.
End Class

// C#
public class Mechanism
{
   internal void AddWidget()
   {
      // Insert code to implement method.
   }
   // Insert code to implement the rest of the class.
}

See Also

internal (C#) | 4.6 Access Types (Visual Basic) | 3.5.2 Accessibility domains (C#) | Component Authoring

Show:
© 2014 Microsoft