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.
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.
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.
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.
In visual basic, we need to follow certain rules to implement a partial class in our applications.
Partial
keyword and all the files must be available at compile time to form the final type.Partial
modifier can only appear immediately before the keywords class, structure, and interface.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.
This is how we can split the functionality of class, structure, interface, or a method over two or more source files using Partial
modifier based on our requirements.