Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I have a console application in that i have one class named class1.cs and i am trying to implement
delegates in it as shown in the code.

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


namespace testingconsoleapplication
{
     delegate int adding(int a, int b);

    class test
    {
        public adding sum = add;


        private static int add(int i, int j)
        {
            return i + j;
        }
        private static int sub(int i, int j)
        {
            return i - j;
        }
    }
}


while this is my program.cs file

C#
namespace testingconsoleapplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 2, j=1;
            test a = new test();
            Console.WriteLine(a.sum.Invoke(i,j));
            Console.ReadLine();
        }
    }
}


Now,it's working fine for only one method and when i try to add another method to delegate variable firstly it dosen't show the "sum" variable in intellisense and secondly it dosent allow to add second method.How can i add second method ?
Posted

1 solution

Are you sure? It works fine for me, (although you don't need the Invoke call)
C#
        int i = 2, j = 1;
        test a = new test();
        Console.WriteLine(a.sum.Invoke(i, j));
        Console.WriteLine(a.nsum.Invoke(i, j));
        Console.WriteLine(a.sum(i, j));
        Console.WriteLine(a.nsum(j, i));
    ...
delegate int adding(int a, int b);

class test
    {
    public adding sum = add;
    public adding nsum = sub;

    private static int add(int i, int j)
        {
        return i + j;
        }
    private static int sub(int i, int j)
        {
        return i - j;
        }
    }

Intellisense shows me both the sum and nsum delegates...
 
Share this answer
 
Comments
Sumit Bhargav 4-Jan-14 4:49am    
hi originalgriff,
thanks for replying
I think you misunderstood the question.I want to pass the second method in the same delegate variable instead of creating a new one like as follow.

public adding sum = add;
sum += sub;

but it dosen't allow it
OriginalGriff 4-Jan-14 5:38am    
That's simple: move the second delegate into the test constructor:
public test()
{
sum += sub;
}
private static int add(int i, int j)
{
Console.WriteLine("add: {0},{1}", i, j);
return i + j;
}
private static int sub(int i, int j)
{
Console.WriteLine("sub: {0},{1}", i, j);
return i - j;
}
(I added the trace statements so you could see that both methods are executed - you can't tell from the return, since it will only be the LAST method that returns a result to the "outside world")
Sumit Bhargav 4-Jan-14 6:21am    
thanks originalgriff that works perfectly but i am still confused why it dosen't add the method outside the constructor?
OriginalGriff 4-Jan-14 6:43am    
Because (with the exception of variable initialization code) you can't execute code outside a method of some description. So trying this:
public adding sum = add;
sum += sub;
Won't compile because the second line is not within a method, and does not declare a variable - so the compiler has no idea when to execute it!
It doesn't have to be within the constructor - any method will do - but since it adds a "handler" to the delegate you probably don't want to do it twice (or you will get two calls to the same method from the delegate execution), so the constructor is the ideal place to do it.
Sumit Bhargav 4-Jan-14 7:03am    
thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900