Visual Basic Partial Method

In visual basic, the Partial method is a special type of method and it will contain a declaration part in one partial class and the definition part in another partial class or in the same partial class.

 

Generally, the partial methods will exist either in partial class or structure and it will contain two main parts one is declaration and definition. Here, the declaration part represents the signature of the partial method and the definition part will represent the implementation of a partial method.

 

In partial method, the definition part is optional so it may or may not have an implementation in partial classes. In case, the implementation part of a partial method is not available in any partial class, then the compiler will remove the definition of the method and all the calls related to that particular method in the final class.

 

Following is the example of implementing a partial method using Partial keyword in two class files, User1.vb and User2.vb.

User1.vb

Partial Public Class User

    ' Partial Method Declaration

    Partial Private Sub GetUserDetails(ByVal x As String, ByVal y As String)

    End Sub

    Public Sub TestMethod()

        Console.WriteLine("Test")

    End Sub

End Class

If you observe the above code, we declared a partial method called GetUserDetails in partial class (User1.vb) using Partial keyword along with the normal method called TestMethod().

User2.vb

Partial Public Class User

    Public Sub New(ByVal a As String, ByVal b As String)

        GetUserDetails(a, b)

    End Sub

    ' Partial Method Implementation

    Private Sub GetUserDetails(ByVal x As String, ByVal y As String)

        Console.WriteLine("Name: " & x)

        Console.WriteLine("Location: " & y)

    End Sub

End Class

If you observe the above code, we implemented partial method called GetUserDetails in partial class (User2.vb) by duplicating the method signature same as declaration with required variables and constructor.

 

When we execute the above code, the compiler will combine these two partial classes into one User class like as shown below.

User

Public Class User

    Public Sub New(ByVal a As String, ByVal b As String)

        GetUserDetails(a, b)

    End Sub

    Public Sub TestMethod()

        Console.WriteLine("Test")

    End Sub

    Private Sub GetUserDetails(ByVal x As String, ByVal y As String)

        Console.WriteLine("Name: " & x)

        Console.WriteLine("Location: " & y)

    End Sub

End Class

This is how the compiler will combine all the partial classes into a single class while executing the application in a visual basic programming language.

Rules to Implement Partial Method

In visual basic, we need to follow certain rules to implement partial methods in our applications.

 

  • In visual basic, the partial method can be implemented within partial class or structure only and the signatures in both parts of the partial type must be the same.
  • The partial method declaration must begin with Partial keyword and method must return void.
  • By default, partial methods are implicitly Private so no access modifiers are allowed and they cannot be Virtual.

Visual Basic Partial Method Example

Following is the example of defining the partial method in partial class using Partial modifier in a visual basic programming language.

 

Module Module1

    Partial Public Class User

        ' Partial Method Declaration

        Partial Private Sub GetUserDetails(ByVal x As String, ByVal y As String)

        End Sub

    End Class

    Partial Public Class User

        Public Sub New(ByVal a As String, ByVal b As String)

            GetUserDetails(a, b)

        End Sub

        ' Partial Method Implementation

        Private Sub GetUserDetails(ByVal x As String, ByVal y As String)

            Console.WriteLine("Name: " & x)

            Console.WriteLine("Location: " & y)

        End Sub

    End Class

    Sub Main()

        Dim u As User = New User("Suresh Dasari", "Hyderabad")

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we created a partial method called GetUserDetails in two partial classes using Partial keyword to perform the required operations.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic (VB) Partial Method Example Result

 

This is how we can use the partial methods in partial class or structure when we have a method declaration and implementation in multiple files based on our requirements.