How to: Create a Partial Method (Visual Basic)

Partial methods give developers a way to insert custom logic into designer-generated code, often for the purpose of data validation. There are two parts to creating a partial method: defining the method signature and writing the implementation. Typically, the definition is written by the designer of a code generator, and the implementation is written by a developer who uses the generated code. For more information, see Partial Methods.

To define the method signature

  1. In a partial class, begin the signature with the keyword Partial.

  2. Use Private as the access modifier.

  3. Add the keyword Sub. The method must be a Sub procedure.

  4. Write the name of the method.

  5. Provide the parameter list for the method.

  6. Finish the method with End Sub.

To implement the method

  1. Use Private as the access modifier.

  2. Add any other modifiers that you want to include.

  3. Write the name of the method, which must match the name in the signature definition.

  4. Add the parameter list. Parameter names must match the names in the signature. Parameter data types can be omitted.

  5. Define the body of the method.

  6. Close with an End Sub statement.

Example

The definition and implementation of a partial method are generally in separate files, which are created by using a partial class. Typically, the purpose of a partial method is to provide notification that something in the project has been changed.

In the following example, a partial method named OnNameChanged is developed and called. The method signature is defined in partial class Customer in file Customer.Designer.vb. The implementation is in partial class Customer in file Customer.vb, and an instance of Customer is created in a project that uses the class.

The result is a message box that contains the following message:

Name was changed to: Blue Yonder Airlines.

' File Customer.Designer.vb provides a partial class definition for 
' Customer, which includes the signature for partial method  
' OnNameChanged. 
Partial Class Customer
    Private _Name As String 
    Property Name() As String 
        Get 
            Return _Name
        End Get 
        Set(ByVal value As String)
            _Name = value
            OnNameChanged()
        End Set 
    End Property 

    ' Definition of the partial method signature. 
    Partial Private Sub OnNameChanged()

    End Sub 
End Class
' In a separate file, a developer who wants to use the partial class 
' and partial method fills in an implementation for OnNameChanged. 
Partial Class Customer

    ' Implementation of the partial method. 
    Private Sub OnNameChanged()
        MsgBox("Name was changed to " & Me.Name)
    End Sub 
End Class
Module Module1

    Sub Main()
        ' Creation of cust will invoke the partial method. 
        Dim cust As New Customer With {.Name = "Blue Yonder Airlines"}
    End Sub 
End Module

See Also

Concepts

Partial Methods

Reference

Partial (Visual Basic)