Partial Methods are one of those technologies that is easiest to understand in two phases. Phase 1 is learning the coding rules for using them. Phase 2 is learning how to use them. Phase 1 comes first because you have to be able to recognize the partial method code before you can focus on the more abstract concepts of using them. At least, that's the way it works for me.
Phase 1 - Coding a Partial Method
First, this technology is only available in VB.NET 2008 and later. It was introduced partly to support LINQ - Language Integrated Query - but there's really nothing that you can do with partial methods that you can't do with earlier versions of VB.NET. Partial methods just make the coding easier and more efficient.
Here are the rules in a nutshell:
1 - As the name implies, a partial method has two parts:
The method signature
The method implementation
Together, they form a complete method - that is, "subroutine", that is approximately the same thing as any other sub.
2 - A partial method signature has to be coded in a partial class
3 - A partial method has to be a subroutine; functions aren't allowed
4 - The partial method signature subroutine has to be empty
5 - The sub that implements a partial method is marked Private
6 - A partial method can only have one implementing method
7 - The definition of the partial method has to be compiled into the same assembly as the implementation
Here's a declaration of a Partial method:
Partial Public Class PartialClassSubs
' Note that the subs are empty
' Not even comments are allowed
Partial Private Sub Tool1()
End Sub
Partial Private Sub Tool2()
End Sub
Partial Private Sub Tool3()
End Sub
End Class
To use one of these partial methods ...
Class Window1
Dim myPartialClass As New PartialClassSubs
Private Sub Tool2()
Console.WriteLine("This implements Tool2")
End Sub
Private Sub Button1_Click( ... <code not shown>
Tool2()
End Sub
End Class
You might be asking yourself, "What's the point?" That's a good question, and it brings us to ...
Phase 2 - How to use partial methods
You're absolutely right if you think that the example above doesn't seem to be very useful. It's designed only to demonstrate the syntax and to be as simple as possible. But notice that there are three partial sub signatures. (Tool1, Tool2, and Tool3.) The implementation only includes Tool2, however. This brings us to one of the principal benefits of partial methods: Only the methods that are implemented get compiled. The first demonstration of this can be seen with Intellisense. The illustration below shows that when only Tool2 has an implementation, only Tool2 is available in Intellisense.
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
Let's go one step deeper to really prove the point. ILDASM is a utility that you can use to look at the compiled code created by Visual Basic .NET. When this program is examined in ILDASM, only Tool2 is there as well. This illustration shows that result:
--------
Click Here to display the illustration
Click the Back button on your browser to return
--------
There's no wasted "overhead" in partial methods at all.
(A note on terminology: You may see the partial method signature referred to as the "stub" as in this sentence taken from another source: "Partial methods are stubbed in one place and implemented in another.")
But we still haven't clearly described what good they are. The best example of that the reason Microsoft added them in the first place: LINQ to SQL. Here's the way Rattz and Hayes describe that in their book, Pro LINQ (reviewed here, if you're thinking of adding it to your library).
"To make the generated entity classes (in LINQ to SQL) more useable, partial methods have been added to them. For example, each mapped property of a generated entity class has a partial method that is called before the the property is changed and another partial method that is called after the method is changed. This allows you to call another module declaring the same entity class, implement the partial methods and be notified every time a property is about to be changed and after it is changed. How cool is that?"
In more detail, LINQ to SQL will generate a region called "Extensibility Method Definitions" that contain partial methods in your assembly. There will be On...Changed and On...Changing partial methods here for each of the properties on the class. If your table involves customers, for example, one of these methods might look something like this:
Partial Private Sub OnCustomerIDChanging(value As Integer)
End Sub
You can code this in your program to validate the CustomerID entity:
Private Sub OnCustomerIDChanging(ByVal value As Integer)
Me.CheckCustomerID(value)
End Sub
(There's a great deal more to it than this. Beth Massi at Microsoft has a much more complete tutorial at MSDN.)
Used this way, they're something like lightweight event generators. The "event" of changing something in the generated LINQ to SQL code can be intercepted and you can add your own code to do something.
