Partial Classes in VB.NET

What they are and how to use them.

Visual Studio
Pantera and Mateusz/Flikr/CC BY 2.0

Partial Classes are a feature of VB.NET that is used almost everywhere, but there's not much written about it. This might be because there are not a lot of obvious "developer" applications for it yet. The primary use is in the way ASP.NET and VB.NET solutions are created in Visual Studio where it's one of those features that is normally "hidden".

A partial class is simply a class definition that is split into more than one physical file.

Partial classes don't make a difference to the compiler because all the files that make up a class are simply merged into a single entity for the compiler. Since the classes are just merged together and compiled, you can't mix languages. That is, you can't have one partial class in C# and another in VB. You can't span assemblies with partial classes either. They all have to be in the same assembly.

This is used a lot by Visual Studio itself, especially in web pages where it is a key concept in "code behind" files. We'll see how this works in a Visual Studio, but understanding what changed in Visual Studio 2005 when it was introduced is a good starting point.

In Visual Studio 2003, the "hidden" code for a Windows application was all in a section called a Region marked "Windows Form Designer generated code". But it was still all there in the same file and it was easy to view, and change, the code in the Region.

All of the code is available to your application in .NET. But since some of it is code that you should <almost> never mess with, it was kept in that hidden Region. (Regions can still be used for your own code, but Visual Studio doesn't use them anymore.)

In Visual Studio 2005 (Framework 2.0), Microsoft did approximately the same thing, but they hid the code in a different place: a partial class in a separate file.

You can see this at the bottom of the illustration below:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

One of the syntax differences between Visual Basic and C# right now is that C# requires that all partial classes be qualified with the keyword Partial but VB does not. Your main form in VB.NET doesn't have any special qualifiers. But the default class statement for an empty Windows application looks like this using C#:

public partial class Form1 : Form

Microsoft's design choices on things like this are interesting. When Paul Vick, Microsoft's VB designer, wrote about this design choice in his blog Panopticon Central, the debate about it in the comments went on for pages and pages.

Lets see how all this works with real code on the next page.

On the previous page, the concept of partial classes was explained. We convert a single class into two partial classes on this page.

Here's an example class with one method and one property in a VB.NET project

 Public Class CombinedClass
    Private m_Property1 As String
 
    Public Sub New(ByVal Value As String)
       m_Property1 = Value
    End Sub
 
    Public Sub Method1()
       MessageBox.Show(m_Property1)
    End Sub
 
    Property Property1() As String
       Get
          Return m_Property1
       End Get
       Set(ByVal value As String)
          m_Property1 = value
       End Set
    End Property
 End Class 

This class can be called (for example, in the Click event code for a Button object) with the code:

 Dim ClassInstance As New _
    CombinedClass("About Visual Basic Partial Classes")
 ClassInstance.Method1() 

We can separate the properties and methods of the class into different physical files by adding two new class files to the project. Name the first physical file Partial.methods.vb and name the second one Partial.properties.vb. The physical file names have to be different but the partial class names will be the same so Visual Basic can merge them when the code is compiled.

It's not a syntax requirement, but most programmers are following the example in Visual Studio of using "dotted" names for these classes. For example, Visual Studio uses the default name Form1.Designer.vb for the partial class for a Windows form. Remember to add the Partial keyword for each class and change the internal class name (not the file name) to the same name.

I used the internal class name: PartialClass.

The illustration below shows all of the code for the example and the code in action.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

Visual Studio "hides" partial classes such as Form1.Designer.vb. On the next page, we learn how to do that with the partial classes we just created.

The previous pages explain the concept of partial classes and show how to code them. But Microsoft uses one more trick with the partial classes generated by Visual Studio. One of the reasons for using them is to separate application logic from UI (user interface) code. In a large project, these two types of code might even be created by different teams. If they're in different files, they can be created and updated with a lot more flexibility.

But Microsoft goes one more step and hides the partial code in Solution Explorer as well. Suppose we wanted to hide the methods and properties partial classes in this project? There's a way, but it's not obvious and Microsoft doesn't tell you how.

One of the reasons you don't see the use of partial classes recommended by Microsoft is that it's not really supported very well in Visual Studio yet. To hide the Partial.methods.vb and Partial.properties.vb classes that we just created, for example, requires a change in the vbproj file. This is an XML file that isn't even displayed in Solution Explorer. You can find it with Windows Explorer along with your other files. A vbproj file is shown in the illustration below.

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

The way we're going to do this is to add a "root" class that is completely empty (only the Class header and End Class statement are left) and make both of our partial classes dependent on it.

So add another class named PartialClassRoot.vb and again change the internal name to PartialClass to match the first two. This time, I have not used the Partial keyword just to match the way Visual Studio does it.

Here's where a little knowledge of XML will come in very handy. Since this file will have to be updated manually, you have to get the XML syntax right.

You can edit the file in any ASCII text editor - Notepad works just fine - or in an XML editor. It turns out that you have a great one in Visual Studio and that's what is shown in the illustration below. But you can't edit the vbproj file at the same time that you're editing the project it's in. So close the project and open only the vbproj file. You should see the file displayed in the edit window as shown in the illustration below.

(Note the Compile elements for each class. DependentUpon sub-elements must be added exactly as shown in the illustration below. This illustration was created in VB 2005 but it has been tested in VB 2008 as well.)

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

For many of us, it's probably enough to know that partial classes are there, just so we know what they are when we're trying to track down a bug in the future. For large and complex systems development, they could be a small miracle because they can help organize code in ways that would have been impossible before. (You can also have partial structures and partial interfaces!) But some people have concluded that Microsoft invented them just for internal reasons - to make their code generation work better.

Author Paul Kimmel even went so far as to suggest that Microsoft actually created partial classes to lower their costs by making it easier to outsource development work around the world.

Maybe. It's the kind of thing they might do.

Format
mla apa chicago
Your Citation
Mabbutt, Dan. "Partial Classes in VB.NET." ThoughtCo, Oct. 24, 2015, thoughtco.com/partial-classes-in-vbnet-3424450. Mabbutt, Dan. (2015, October 24). Partial Classes in VB.NET. Retrieved from https://www.thoughtco.com/partial-classes-in-vbnet-3424450 Mabbutt, Dan. "Partial Classes in VB.NET." ThoughtCo. https://www.thoughtco.com/partial-classes-in-vbnet-3424450 (accessed December 18, 2017).