Defination :
Partial Class allows to split definition of classes, interfaces and structures over more than one files.The compilers for VB.Net or C# look for the partial classes and integrate them while compiling, to form the intermediate language. This intermediate language is the same when compared to the intermediate language that is generated, if all the partial classes are combined to form a single class in a single file. There is no modification done in the CLR for the implementation of partial classes.
This benefits of partial classes is "
1 - It allows a clean separation of business logic and the user interface
2 - It allows programmers to work simultaneously on different parts of a class without needing to share the same physical file.
3 - You can easily write your code for extended functionality for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.
Code Example :
'Class 1 : To Define the Class, Class variables and Constructors
Imports Microsoft.VisualBasic
Public Class Calcuator
Dim Param1 As New Decimal
Dim param2 As New Decimal
Dim strMsg As String
Public Sub New()
Param1 = 0
param2 = 0
End Sub
Public Sub New(ByVal x As String, ByVal y As String)
'fnCheckData : It uses the method defined in Partial Class 2
If fnCheckData(x, y) Then
Param1 = x
param2 = y
End If
End Sub
End Class
'Class 2 : To Validate the value for Class variables
Imports Microsoft.VisualBasic
Partial Public Class Calcuator
Public Function fnCheckData(ByVal x As String, ByVal y As String) As Boolean
If IsNumeric(x) And IsNumeric(y) Then
fnCheckData = True
End If
Return fnCheckData
End Function
End Class
'Class 3 : All the Business Logics are written here.
'It uses the Class variables defined in Partial Class 1
Imports Microsoft.VisualBasic
Partial Public Class Calcuator
Public Function Add() As Decimal
Add = Param1 + param2
Return Add
End Function
Public Function SubStract() As Decimal
SubStract = Param1 - param2
Return SubStract
End Function
Public Function Division() As Decimal
If param2 <> 0 Then
Division = Param1 / param2
End If
Return Division
End Function
Public Function Multiplication() As Decimal
Multiplication = Param1 * param2
Return Multiplication
End Function
End Class
'Form : Where Partial Class Implemented
Partial Class Default5
Inherits System.Web.UI.Page
'Create the partial class object. It Uses Partial Class 1
Dim calc As New Calcuator()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Reintialise the partial class object. It Uses Partial Class 1
calc = New Calcuator(TextBox1.Text, TextBox2.Text)
End Sub
'Implement the partial class Methods for the Respective Button Click. It Uses Partial Class 3
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
lblResult.Text = calc.Add()
End Sub
Protected Sub btnSub_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSub.Click
lblResult.Text = calc.SubStract()
End Sub
Protected Sub btnMul_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMul.Click
lblResult.Text = calc.Multiplication()
End Sub
Protected Sub btnDiv_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDiv.Click
lblResult.Text = calc.Division()
End Sub
End Class