Example to Creating a Component / Assembly



Class Diagram :-





Steps to Create an Assembly

Click on File
1.     New
2.     project select Visual C# 
3.     select Class Library template Type the class library or assembly Name (CLibMaths)
4.     Click on Ok                                    
5.Change the class name to ClsArithmetic

STEPS:-

1)


2)



Write the following code in Source
 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CLibMaths
{
    public class ClsArithematic
    {
        int Num1, Num2, Result;
        public int PNum1
        {
            set
            {
                Num1 = value;
            }
        }
        public int PNum2
        {
            set
            {
                Num2 = value;
            }
        }
        public int PResult
        {
            get
            {
                return Result;
            }
        }
        public void Add()
        {
            Result = Num1 + Num2;
        }
        public void Subtract()
        {
            Result = Num1 - Num2;
        }
        public void Multiply()
        {
            Result = Num1 * Num2;
        }
        public void Divide()
        {
            Result = Num1 / Num2;
        }

    }
}



  • Add a new class with name ClsMathNumbers
  • Change the accessibility of the class to public and write following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CLibMaths
{
   public class ClsMathNumbers
    {
        public int Square(int x)
        {
            return x * x;
        }
        public int Cube(int x)
        {
            return x * x * x;
        }
        public int Factorial(int x)
        {
            int R = 1;
            for (int i = 1; i <= x; i++)
            {
                R = R * i;
            }
            return R;
        }

    }
}



Build the solution, this will create a DLL. This dll is known as .NET component
To see the dll go the \bin\Debug folder of the application, you find the CLibMaths.dll

 Consuming the Component/Assembly as Private
  • Create a console Application with the name CACLibMathPrivate
  • Change class name to ClsSamplee
  • To consume any DLL /Component first we need to add reference to the DLL

 Steps:-
1)

 2)



Steps to Add the Reference

1)    Go to Solution Explorer
2)    Select the solution
3)    Click with right mouse button
4)    Click on Add Reference
5)    Click on Browse
6)    Go to the Location, where dll is saved [C:\Users\cnu\Downloads\sampath kumar\CLibMaths\CLibMaths\bin\Debug]
7)    Select the CLibMaths.dll and click on Ok

Steps:-

1)


 2)

3)


Write the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CLibMaths;
namespace CACLibPrivate
{
    class ClsSamplee
    {
        static void Main(string[] args)
        {
            ClsArithematic obj1 = new ClsArithematic();
            ClsMathNumbers obj2 = new ClsMathNumbers();
            Console.WriteLine("Enter Any Two Numbers :- ");
            obj1.PNum1 = Convert.ToInt32(Console.ReadLine());
            obj1.PNum2 = Convert.ToInt32(Console.ReadLine());
            obj1.Add();
            Console.WriteLine("Sum of two Numbers is        :- "+obj1.PResult);
            obj1.Subtract();
            Console.WriteLine("Diffrence of two Numbers is  :- " + obj1.PResult);
            obj1.Multiply();
            Console.WriteLine("Product of Two Numbers       :- "+obj1.PResult);
            obj1.Divide();
            Console.WriteLine("Quotient is   :- " + obj1.PResult);
            Console.WriteLine("Enter Any Number :- ");
            int s = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Square value is   :- " + obj2.Square(s));
            Console.WriteLine("Cube Value is     :- " + obj2.Cube(s));
            Console.WriteLine("Factorial Value is:- " + obj2.Factorial(s));
            Console.ReadLine();
        }
    }
}



Output :-
   




No comments:

Post a Comment