Tuesday, May 19, 2020
Overview of Partial Classes in Visual Basic .NET
Partial Classes are a feature of VB.NET that is used almost everywhere, but theres 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 its 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 dont 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 cant mix languages. That is, you cant have one partial class in C# and another in VB. You cant 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. Well 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 doesnt 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 illustrationClick 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 doesnt have any special qualifiers. But the default class statement for an empty Windows application looks like this using C#: public partial class Form1 : Form Microsofts design choices on things like this are interesting. When Paul Vick, Microsofts 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. Heres 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. Its 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 illustrationClick 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 theyre 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? Theres a way, but its not obvious and Microsoft doesnt tell you how. One of the reasons you dont see the use of partial classes recommended by Microsoft is that its 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 isnt 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 illustrationClick the Back button on your browser to return-------- The way were 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. Heres 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 thats what is shown in the illustration below. But you cant edit the vbproj file at the same time that youre editing the project its 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 illustrationClick the Back button on your browser to return-------- For many of us, its probably enough to know that partial classes are there, just so we know what they are when were 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. Its the kind of thing they might do.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.