Visual Basic Partial Class

In visual basic, Partial Class is useful to split the functionality of 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 wants to work on the same class file at the same time. To solve this problem, visual basic provides an ability to spread the functionality of particular class into multiple class files using Partial keyword.

 

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

 

Following is the example of splitting the definition of 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 like 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 single class while executing the application in visual basic programming langauge.

Rules to Implement Partial Class

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

 

  • To split the functionality of classstructureinterface or a 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 classstructureinterface.
  • 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.
  • If any partial part is declared as Abstract, Sealed or Base, then the whole type is considered as Abstract or Sealed or Base based on the defined type.
  • In visual basic, different parts can have different Base types but the final type will inherit all the base types.
  • Nested partial types are allowed in partial type definitions.

Visual Basic Partial Class Example

Following is the example of defining a partial classes using Partial keyword in 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 single class to perform the required operations.

 

When we execute the above visual basic program, we will get the result like 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.