Aim: - Write a program to create a DLL perform addition and substraction of two number.
Steps :-
Step 1 - Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" -> "Class library".
Step 2 - In the Class1 class, write methods for the addition and subtraction of two integers.
Source Code:-
using System;
namespace ClassLibrary2
{
public class Class1
{
public int Add(int a, int b)
{
return a + b;
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
Step 3 - Build the solution (F6). If the build is successful then you will see a "ClassLibrary2.dll" file in the "bin/debug" directory of your project.
Finally, we have created our DLL file. Now we will use it in another application.
Using DLL File
Step 1 - Open Visual Studio then select "File" -> "New" -> "Project..." then select "Visual C#" > "ASP.Net Web Project".
Step 2 - Design the form:-
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="DLL.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
}
.auto-style2 {
width: 99px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="auto-style1">
<tr>
<td class="auto-style2">
<asp:Label ID="Label1" runat="server" Text="Enter 1st No."></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" Height="20px"
Width="163px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Label ID="Label2" runat="server" Text="Enter 2nd No."></asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" Width="161px"
Height="20px"></asp:TextBox>
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="Addition" Width="86px" />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click"
Text="Subtraction" Width="91px" />
</td>
</tr>
<tr>
<td class="auto-style2"> </td>
<td> </td>
</tr>
<tr>
<td class="auto-style2">
</td>
<td>
<asp:Label ID="Label3" runat="server" Text=" Result :"></asp:Label>
<asp:Label ID="LabelResult" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Step 3 - Add a reference for the dll file, “ClassLibrary2.dll” that we created earlier. Right-click on the project and then click on "Add reference".
Step 4 - Select the DLL file and add it to the project.
Step 5 - Add the namespace ("using ClassLibrabry2 ;")
Source Code:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary2;
namespace DLL
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
Class1 cal = new Class1();
protected void Button1_Click(object sender, EventArgs e)
{
try
{
int i = cal.Add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
LabelResult.Text = i.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
protected void Button2_Click(object sender, EventArgs e)
{
try {
int i = cal.Sub(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
LabelResult.Text = i.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
1 Comments
Useful Information
ReplyDelete