![]() |
|
|
Partial Methods in C# 3.0 and VB.net 9.0 |
Post Reply
|
| Author | |
Jijo
Admin Group
Joined: 31 Dec 05 Location: United Kingdom Posts: 495 |
Quote Reply
Topic: Partial Methods in C# 3.0 and VB.net 9.0Posted: 03 Sep 07 at 7:15am |
|
Partial Methods in C# 3.0 and VB.net 9.0
Partial methods are a new language feature in both VB.net and C# and are designed to enable lightweight event handling and hooks into the class. The code samples are in C#, but I have provided a link at the bottom for the VB.net version - the principals are the same. Few important points: Partial methods can only be defined or implemented within a partial class. They must return void however can accept arguments (ref parameters but not out parameters). Must be private Consider the following C# code which has a simple getter and setter for the name of a person. partial class Person { string name; public string Name { get { return name; } set { OnNameChanging(value); name = value; OnNameChanged(); } } partial void OnNameChanging(string name); partial void OnNameChanged(); } When a name is set, a call is made to the OnNameChanging method, passing in the value as a parameter. After the name has changed the OnNameChanged method is called. But these methods are just placeholders/interfaces which could be implemented by a developer. In a separate partial class we can then implement this functionality which will handle what we want to happen when the methods are called. partial class Person { partial void OnNameChanged() { Console.WriteLine("OnNameChanged()"); } partial void OnNameChanging(string name) { Console.WriteLine("OnNameChanging(string name)"); Console.WriteLine(name); } } Now, when we compile, everything will be merged and act as a single class. If we then set the name on the object, the following is wrote out to the console OnNameChanging(string name) Ben Hall OnNameChanged() But what what happens if no partial class implements the method body for a partial method? Well, the compiler removes calling reference from the code and does not get compiled. If the two methods where not implemented, the setter would look like this: set { name = value; } VB.net Sample Module Module1 Sub Main() Console.WriteLine("Starting") Dim p As Person p = New Person() p.Name = "Test" Console.ReadLine() End Sub End Module Partial Class Person Partial Private Sub OnNameChanging(ByVal name As String) End Sub Partial Private Sub OnNameChanged() End Sub Dim _name As String Property Name() As String Get Return _name End Get Set(ByVal value As String) OnNameChanging(value) _name = value OnNameChanged() End Set End Property End Class partial Class Person Private Sub OnNameChanging(ByVal name As String) Console.WriteLine("Name Changing " & name) End Sub Private Sub OnNameChanged() Console.WriteLine("The Name was changed") End Sub End Class What is new in VB 2008
What are Extension Method
Nullable Types in VB 2008 & VB 2005
IIf in VB2005 vs IF in VB2008 - Whats new in VB9.0
Relaxed Delegates. VB 2008 / VB 9.0
|
|
![]() |
|
Post Reply
|
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |