As I have
earlier mentioned about Partial Class in our previous post. Here I am going to
explain you how partial class works with in our live scenario. Here is the
simple demonstration; I hope you will like it...
Step 1:
I am going to create partial Class (ClassA) with the class name ‘Calculation’, below
is the code:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
///
/// partial class using same name (Calculation)
///
///
//variable declaration in this partial class
(ClassB)
public partial class Calculation
{
public int Num1 = 0;
public int Num2 = 0;
public int Res = 0;
public string
Operator = null;
}
Step 2: I
am going to create partial Class (ClassB) with the same class name ‘Calculation’,
below is the code:
using
System;
using
System.Data;
using
System.Configuration;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
///
/// partial class using same name (Calculation)
///
///
public partial class Calculation
{
//Function on this partial class (ClassB)
public void
Calculation()
{
switch (Operator)
{
case "+":
Res =
Num1 + Num2;
break;
case "-":
Res =
Num1 - Num2;
break;
case "*":
Res = Num1 * Num2;
break;
case "/":
Res =
Num1 / Num2;
break;
case "%":
Res =
Num1 % Num2;
break;
}
}
}
Step 3: Here
is the code how we are going to use the Partial class in our application:
Here is the .aspx code:
<div>
<asp:TextBox ID="txtNum1"
runat="server">asp:TextBox> <asp:TextBox ID="txtNum2" runat="server">asp:TextBox><asp:DropDownList ID="ddlOperator"
runat="server">
<asp:ListItem Value="+">Additionasp:ListItem>
<asp:ListItem Value="-">Substractionasp:ListItem>
<asp:ListItem Value="*">Multiplicationasp:ListItem>
<asp:ListItem Value="/">Dividationasp:ListItem>
<asp:ListItem Value="%">Modulusasp:ListItem>
asp:DropDownList>
<asp:Button ID="btnAction"
runat="server"
Text="Action"
onclick="btnAction_Click"
/>
<asp:Label ID="lblDisplay"
runat="server">asp:Label>
div>
Here is the .aspx.cs code
using
System;
using
System.Collections;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
public partial class PartialClassEx : System.Web.UI.Page
{
Calculation objEmp = new
Calculation(); //Partial class
initialization
protected void
Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
protected void
btnAction_Click(object sender, EventArgs e)
{
objEmp.Num1 = Convert.ToInt32(txtNum1.Text); //assign value
(num1) to object classA
objEmp.Num2 = Convert.ToInt32(txtNum2.Text); //assign value
(num2) to object classA
objEmp.Operator
= ddlOperator.SelectedValue; //assign operator (function) to object classA
objEmp.Calculation(); //calculation in partial ClassB
lblDisplay.Text
= Convert.ToString(objEmp.Res); //Result from partial ClassA (Set value from ClassB) after
calculation
}
}
I hope
now you are very much clear about partial classes.
Please
feel free to comment / feedback to this post to make it more efficient in
future. Thank you very much.
Comments