You must Sign In to post a response.
  • Category: .NET

    How to use call delegates from class file to aspx.cs file

    Dear Friends
    I have write delegates in class file and also have more than 10 method. now i want to use that 10 method in aspx.cs page using delegates object. how its possible..
  • #766504
    A delegate can point to multiple methods but all these methods must have same signature as the delegate. Have you written a single delegate and ten methods. These ten methods do they have to be called from the same delegate.

    If yes then when you instantiate the different instances of the delegate you need to pass the method you want to call while instantiating. Then call the invoke method to call the respective method.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #766553
    Hi,

    Kindly use the following Links for your Requirement.

    1.http://www.codeproject.com/Articles/109000/C-Delegates-Step-by-Step

    2.http://www.c-sharpcorner.com/UploadFile/0c1bb2/calling-usercontrol-method-from-parent-page-in-Asp-Net/

    Regards,
    Karunanidhi.K

  • #766597
    1. Initialize the delegate.
    2. Create Method(functions)
    3. Point the Function through the delegate.

    Following sample code will give your more information.

    public delegate double Delegate_Multi(int a,int b);
    class Class1
    {
    static double MultiplayvaluesFN(int val1,int val2)
    {
    return val1*val2;
    }
    static void Main(string[] args)
    {
    //Creating the Delegate Instance
    Delegate_Multi delObj = new Delegate_Multi(MultiplayvaluesFN);
    Console.Write("Please Enter Values");
    int v1 = Int32.Parse(Console.ReadLine());
    int v2 = Int32.Parse(Console.ReadLine());
    //use a delegate for processing
    double res = delObj(v1,v2);
    Console.WriteLine ("Result :"+res);
    Console.ReadLine();
    }
    }

    By Nathan
    Direction is important than speed

  • #766623
    Whenever we want to create delegate methods we need to declare with delegate keyword and delegate methods signature should match exactly with the methods which we are going to hold like same return types and same parameters otherwise delegate functionality won't work if signature not match with methods
    see below sample
    write a delegate in class

    public delegate int Delegatmethod(int a,int b);
    public class Sampleclass
    {
    public int Add(int x, int y)
    {
    return x + y;
    }
    public int Sub(int x, int y)
    {
    return x + y;
    }
    }

    you can create a class object and add reference of that class in asp.net and then by creating object of that class you can call it in asp.net

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766626
    Hi

    try this code here i mention 3 methods if you need add more



    protected void Page_Load(object sender, EventArgs e)
    {
    SampleMethod sc = new SampleMethod();

    DelegatMethod delgate1 = sc.Add;
    int i = delgate1(10, 20);
    Console.WriteLine(i);
    DelegatMethod delgate2 = sc.Sub;
    int j = delgate2(20, 10);

    DelegatMethod delgate3 = sc.Multiply;
    int z = delgate3(20, 10);
    }

    public delegate int DelegatMethod(int a, int b);
    public class SampleMethod
    {
    public int Add(int x, int y)
    {
    return x + y;
    }
    public int Sub(int x, int y)
    {
    return x - y;
    }

    public int Multiply(int x, int y)
    {
    return x - y;
    }
    }


    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments