Visual Basic Partial Class

In visual basic, Partial Class is useful to split the functionality of a particular class into multiple class files and all these files will be combined into one single class file when the application is compiled.

 

While working on large-scale projects, multiple developers want to work on the same class file at the same time. To solve this problem, visual basic provides the ability to spread the functionality of a particular class into multiple class files using Partial keyword.

 

In visual basic, we can use Partial keyword to split the definition of a particular class, structure, interface, or method over two or more source files.

 

Following is the example of splitting the definition of the User class into two class files, User1.vb and User2.vb.

User1.vb

Partial Public Class User

    Private name As String

    Private location As String

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

        Me.name = a

        Me.location = b

    End Sub

End Class

If you observe the above code, we created a partial class called User in User1.vb class file using Partial keyword with required variables and constructor.

User2.vb

Partial Public Class User

    Public Sub GetUserDetails()

        Console.WriteLine("Name: " & name)

        Console.WriteLine("Location: " & location)

    End Sub

End Class

If you observe the above code, we created a partial class called User in User2.vb class file using Partial keyword with GetUserDetails() method.

 

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

User

Public Class User

    Private name As String

    Private location As String

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

        Me.name = a

        Me.location = b

    End Sub

    Public Sub GetUserDetails()

        Console.WriteLine("Name: " & name)

        Console.WriteLine("Location: " & location)

    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 Class

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

 

  • To split the functionality of a classstructureinterface, or method over multiple files we need to use Partial keyword and all the files must be available at compile time to form the final type.
  • The Partial modifier can only appear immediately before the keywords classstructure, and interface.
  • All parts of partial type definitions must be in the same namespace or assembly.
  • All parts of partial type definitions must have the same accessibility, such as Public, Private, etc.
  • In visual basic, all partial types must inherit from the same base type because a class can inherit from a single base class.
  • Nested partial types are allowed in partial type definitions.

Visual Basic Partial Class Example

Following is an example of defining partial classes using Partial keyword in a visual basic programming language.

 

Module Module1

    Partial Public Class User

        Private name As String

        Private location As String

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

            Me.name = a

            Me.location = b

        End Sub

    End Class

    Partial Public Class User

        Public Sub GetUserDetails()

            Console.WriteLine("Name: " & name)

            Console.WriteLine("Location: " & location)

        End Sub

    End Class

    Sub Main()

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

        u.GetUserDetails()

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

        Console.ReadLine()

    End Sub

End Module

If you observe the above example we created a partial class User using Partial keyword and we are able to access all the partial classes as a single class to perform the required operations.

 

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

 

Visual Basic Partial Class Example Result

 

This is how we can split the functionality of classstructureinterface, or a method over two or more source files using Partial modifier based on our requirements.