You are here:About>Computing & Technology>Visual Basic> Using VB.NET> Partial Classes in Visual Basic .NET 2005
About.comVisual Basic
Newsletters & RSSEmail to a friendSubmit to Digg

Partial Classes in Visual Basic .NET 2005

From Dan Mabbutt,
Your Guide to Visual Basic.
FREE Newsletter. Sign Up Now!

The "Stealth" Feature in VB.NET 2005

One feature of VB.NET 2005 has the potential to revolutionize the way the language works but strangely, is under the radar of most of the books and articles. For example, I can't even find it mentioned in the new Microsoft Press book, Introducing Microsoft Visual Basic 2005 for Developers.

I shouldn't talk, however. I missed it as well in my article, What's New in Visual Basic 2005!.

This might be because there are not a lot of obvious "developer" applications for it yet. But I can't stop wondering whether it's missing from a Microsoft Press book because Microsoft just isn't encouraging developers to use partial classes. Although they're well written, MSPress usually follows the "party line" pretty closely and development techniques that don't fit with Microsoft's marketing plan are often misssing. The implementation of partial classes is still a bit kludgy for developers right now and it's used primarily by Visual Studio itself so far. More about that in just a moment.

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 grouped into a single entity. Since the classes are just merged together and compiled, you can't mix languages. That is, you can't have one part of a class in C# and another in VB.

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 2005, but understanding what changed from Visual Studio 2003 is a good starting point.

In Visual Studio 2003, the form code for a blank Windows application looks like the top illustration below. One of the dramatic improvements in .NET is that all of the code is available to your application. But since some of it is code that you should <almost> never mess with, it's kept in a hidden Region section. My article Regional Issues - What's in the hidden Region in VB .NET? explained how the Region directive works.

In Visual Studio 2005, Microsoft is doing approximately the same thing. They're just hiding 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. The 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. Even though they don't follow this rule with Visual Studio generated partial classes, if you code your own partial classes Microsoft recommends that you use the C# convention and identify all of your partial classes with the keyword Partial.

Lets see how all this works with real code.

 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.