A group blog from members of the VB team
About two weeks ago I had the opportunity to attend the product group dinner associated with the Global MVP Summit that was behind held here at Microsoft. Besides a free meal, this also gave me the excellent opportunity to meet with some of our MVPs and discuss their impressions with Visual Studio and all the cool new features we are introducing for Orcas. At the dinner, I had an opportunity to talk with a group of VB MVPS from Japan, who met with me and about 5 or 6 other members of the VB team. While we were talking, a couple of them asked me a few questions about partial methods. Unfortunately, I do not speak Japanese, and so had some trouble answering them. Figuring that written English may be easier to understand than my idiomatic spoken English, I decided to make Partial Methods the topic of my next blog post.
As a result, I've included their questions below along with my answers.
What are Partial Methods?
In a nut-shell, partial methods are a light-weight replacement for events designed primarily for use by automatic code generators. They are declared by creating a private method with an empty body and decorating it with the Partial keyword. The method may then be "re-implemented" elsewhere within its containing class. If the method is implemented, then the compiler will redirect all calls to the partial method to the implementing method. If the method is not implemented in its containing class, then the compiler silently removes any calls to it from the program. They differ from events in several ways, mainly:
An example use of partial methods is shown below:
'Designer File Partial Class DesignerGeneratedClass Partial Private Sub OnFoo() End Sub Partial Private Sub OnBar() End Sub Sub DoSomething() OnFoo() OnBar() End Sub End Class
'Code Behind File Class DesignerGeneratedClass Public Overridable Sub OnFoo() Console.WriteLine("Foo Occured") End Sub End Class
Here we define a class named DesignerGeneratedClass split into two partial classes. It is designed to mimic typical code generation scenarios where one partial class contains designer generated code and another contains user generated code. For example, Windows Forms classes and pages in ASP.NET Web Application Projects utilize this approach. In the designer generated file we define a utility method, DoSomething, that calls two partial method declarations OnFoo and OnBar. The user file implements OnFoo, but not OnBar. When the VB compiler processes this class, it notices this and emits a body for DoSomething() that only calls OnFoo().
What are Partial Methods useful for?
For most applications, events are almost always a better choice than partial methods due to their increased flexibility. However, there are some scenarios where they come in handy. A couple of them are listed below:
Are Partial Methods Some Form Of AOP (Aspect Oriented Programming)?
No. Partial methods do share some things in common with Aspect Oriented Programming in that partial methods offer a way of associating "hooks" with custom code. However, unlike AOP systems partial methods require extension points to be explicitly defined by the customer. AOP systems allow developers to "weave in" functionality into code that hasn't had explicit hooks defined. Usually they do this by specifying some declarative search strings that describes the places where the hooks should be applied. The compiler is then responsible for finding all the appropriate "extension points" to apply the hooks to and injecting any necessary method calls. Partial methods in VB do not do this.
Is "Partial Methods" the final name for this feature? When I heard the name I could not imagine how it worked, but I had a completely different view on it. Consider the name "Optional Method" or something else (like Partial, Optional is also a known VB name).
I don't think your example is a very good example of partial methods. You can already achieve the conditional output of a method call by using the Conditional attribute (which is a lot nicer solution).
<Conditional("DEBUG")> _
Private Sub LogMessage(ByVal s as String)
Console.WriteLine(s)
End Sub
Will there be support for partial properties too?
David, using the ConditionalAttribute would work for the DoStuff method but you would still have to leave the LogMessage method in your code. If you wanted to exclude the LogMethod from your code, then this approach achieves it (albeit by exchanging it for a partial method declaration).
Not a huge difference, I agree, but a difference nonetheless :)
少し前から話題になっている Partial Method ですが、グローバルサミットの時にVBチームの方に質問した内容の返答がブログに掲載されています!!すげーー!! http://blogs.msdn.com/vbteam/archive/2007/03/27/partial-methods.aspx Partial Method に関して何のためにいつ使うのか、もしかして AOP っぽいもの?っていうのを尾崎さんに通訳をしてもらいながら質問しました。 その時は会話だけでコミュ...
I am MVP of Japan that questioned you. It read very interesting.
Thank you.
PartialMethod was understood as the thing to add mounting the method
according to the situation.
I think that the usage of substitution from the If Else macro is very
good.
However, timing and the usage used are a little difficult.
The designer uses it for the code generation.
The developer doesn't use it so much.
Klaus,
I beleive "partial methods" is the final name for the feature. We picked it because of its close association with partial classes.
Nick,
No, there will not be support for partial properties. In general partial methods cannot have return types, so the only kind of partial properties we would be able to support would be write-only properties, which are not that useful.
Not supporting return types is a bit of a missed opportunity though.
Can't this be achieved by using a syntax construct in which there is no method body (like an abstract method) as opposed to an empty one?
Cheers
I don't want this feature owned by C# .
The blog front: Scott Guthrie: ASP.NET AJAX Documentation Update, Videos and Cool Articles Mix of ASP.NET
こんにちは、VSUGスタッフの杉山です。 少し前の話ですが、3月の中旬にマイクロソフト本社で行われた Global Summit に参加してきました。僕自身初海外ということでかなり緊張しての参加だったのですが、レベルの高いセッションを受けられたこと、多くの MVPの方と交流を持てたこと、向こうのセッションに直に触れられたことの興奮で緊張なんてスッカリ忘れ、充実した4日間を過ごさせてもらいました。 ...
Jun2007 Orcas CTP では以前話題になった Partial Method が C#, VB.NETともに実装されています。
Hooray! Visual Basic 2008 Beta2 has been released to the wild today to return to its natural habitat
A good question on Partial Methods Yesterday in Kansas City I got a question during the LINQ session