.NET Tips and Tricks

Blog archive

Use Nested Classes to Organize Complex Classes

The ability to nest one class inside provides a powerful way to encapsulate code inside a class to organize complex functionality used within a single class. A nested class looks like this:

Public Class MyClass
  …code for the class…  

  Class MyNestedClass

  End Class
  
  Public Sub SomeMethod()
     Dim mnc As MyNestedClass
     mnc = New MyNestedClass()
     …using the nested class…
  End Sub

End Class

As the example shows, in the surrounding host class you can instantiate the nested class and use it. No other class, however, can access the nested class. Nested classes give you a way of organizing the functionality of the host class without exposing that functionality to other classes (or cluttering up your IntelliSense lists with new classes that are only useful in one place -- the host class).

For instance, in a recent application, I needed to manage the permissions and privileges for three levels of users in a form. I created a class for each type of user, with each class holding all (and only) the code for managing what that type of user was allowed to do. Any permissions-related code that was shared by all the users I put in a base class that the three user classes inherited from, giving me four classes altogether. This design cleaned up and organized my permissions code while simplifying the code in the host that set permissions in the form's user interface: the host class just instantiated the right nested class and called its permissions-related members.

Since these classes were only relevant to that single form, I nested all four classes within the controller for that form. With that design, code in the form could instantiate the nested classes and use their members, but the classes didn't appear in any IntelliSense lists outside of the host class.

If you do need to make a nested class available to the outside world, you can add a property to your host class that returns the nested class (as I did in a recent column on creating a fluent interface).

And, of course, you can argue that any class that needs nested classes is too big and too complicated, and is probably violating the Single Responsibility Principle. But that's a different discussion.

Posted by Peter Vogel on 01/09/2014


comments powered by Disqus

Featured

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube