Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 31-10-2009
Member
 
Join Date: Jun 2009
Posts: 3,960
Generic delegate in C#

I have a small concern about generic delegates in C#. Here's the context:

I created 2 classes:

- GenericCallbacks: By default a method pointer and the call when asked.

- TestDelegate: It creates GenericCallbacks and methods of test attached to delegates.

Here is the complete code compiled, I will issue the following:

GenericCallbacks:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGenericDelegate
{
    class GenericCallbacks
    {
        private Delegate action;
        private int x;
        private int y;
        public delegate void Callback();
        public delegate void Callback2(int x, int y);
        /*
         * [...]
         * Many delegate prototyping different
         * Not very clean..
         */
        public GenericCallbacks(Delegate Action)
        {
            this.action = Action;
            this.x = this.y = 0;
        }
    //    public GenericCallbacks(Callback2 Action)
     //   {
     //       this.action = Action;
     //       this.x = this.y = 5;
     //   }
        public void Invoke()
        {
            /* After various operations on x and y means I know what cast */
            if (this.x != 0 && this.y != 0)
            {
                ((Callback2)action).Invoke(x, y);
            }
            else
            {
                ((Callback)action).Invoke();
            }
        }
    }
}
TestDelegate :

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGenericDelegate
{
    class TestDelegate
    {
        public void Method1()
        {
            Console.WriteLine("Method 1." );
            return;
        }
        public void Method2(int x, int y)
        {
            Console.WriteLine("Method 2 : {0}, {1}.", x, y);
            return;
        }
        public TestDelegate()
        {
            GenericCallbacks unitTest = new GenericCallbacks(Method1);
            GenericCallbacks unitTest2 = new GenericCallbacks(Method2);
            unitTest.Invoke();
            unitTest2.Invoke();
        }
    }
}
The main test:

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGenericDelegate
{
    class Program
    {
        static void Main(string[] args)
        {
            TestDelegate testDelegate = new TestDelegate();
            Console.ReadKey(true);
        }
    }
}
The problem is as follows:

How to avoid creating so many constructors of delegate type in the class GenericCallbacks?

In the example I only have 2, but in the real program that asks me worry I have about 10, knowing that I have 1 overload for each type of delegates which gives 20 overload for the same how to add!

In my case, even if it's ugly, it might work, but I dare not imagine that the developer is left with 250 possible prototype for its delegates.

I tried to make the constructor a type System.Delegate but it is not working.
I'm looking for a generic type that can contain a pointer to method but I found none.
Reply With Quote
  #2  
Old 31-10-2009
Member
 
Join Date: Nov 2008
Posts: 1,192
Re: Generic delegate in C#

It has been a long time since I have not touched C#, but I really find it difficult to understand in which case you might want to abstract the prototype of your delegates
Reply With Quote
  #3  
Old 31-10-2009
Member
 
Join Date: May 2008
Posts: 685
Re: Generic delegate in C#

I do not understand what you're looking for and what you need may be simpler also. But then also a small remark on your example:

Code:
if (this.x != 0 && this.y != 0)
{
    ((Callback2)action).Invoke(x, y);
}
else
{
    ((Callback)action).Invoke();
}
It can be transformed into this :

Code:
if (action is Callback2)
{
    ((Callback2)action).Invoke(x, y);
}
else if(action is Callback)
{
    ((Callback)action).Invoke();
}
If passed, as explained in another topic, you can free yourself writing multiple vendors. You can assign variables without constructors, like this:

Code:
GenericCallbacks instance = new GenericCallbacks()
{
    x = 1,
    y= 2
};
In this case, it is necessary that the variables x and y are public.

Another possibility: use the keyword params to specify a constructor (or method) can have any number of parameter type.
Ex:

Code:
public class myClass
{
    public myClass(params int[] list)
    {
        int a = list[0];
    }
}

myClass myClass1 = new myClass(1, 2, 3, 4);
Reply With Quote
  #4  
Old 31-10-2009
Member
 
Join Date: Jun 2009
Posts: 3,960
Re: Generic delegate in C#

Thank you for your reply, I did not know these techniques very useful. In contrast, what is the purpose of the manipulation is simply to avoid declaring delegate method 1. In my case it was for the addition of a control, and I find myself using methods like:

Code:
public void AddControl(string Name, Keys Keys, EmptyCallback Action, bool KeyRepeat);
"EmptyCallback" and type void (), but I have several different delegate.
I have also overload of this method (AddControl) to take a picture of Keys.

So I find myself with 2 methods AddControl type of delegate.
With 10 types of delegate instance, you can imagine the carnage ...

I was simply trying to clarify the code because I found it very dirty.

Otherwise the technique by fellah statement is interesting, but the fact of putting its attributes in public is not contrary to the concept object? (encapsulating all that)
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Generic delegate in C#"
Thread Thread Starter Forum Replies Last Post
Issues in Outlook 2007 while trying to delegate information jhon Windows Software 5 20-03-2010 02:32 AM
Delegate Control of OU in AD 2008 Damien25 Active Directory 1 05-02-2010 06:32 PM
where to look for delegate in Active Directorey Gaurav Bhardwaj Active Directory 8 13-06-2009 03:45 AM
Delegate permission let one user to join pc to a domain Bhuvan Active Directory 3 19-08-2008 02:21 AM
Delegate exchange tasks Manik Active Directory 4 18-03-2007 10:36 AM


All times are GMT +5.5. The time now is 11:57 PM.