+ Reply to Thread
Results 1 to 2 of 2

Thread: Delegates

  1. #1
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Delegates

    Delegate is basically a function definition that will define how a function will return (the return type int) and the parameters passed to it. It is very similar to a function pointer in C++. The great thing is is that at compile time the compiler does not know what method is actually going to be called until run time.

    The delegate is just like any other method in its syntax apart from the word delegate is present e.g.

    Code:
    public delegate int functionName(parameters);
    
    // in this example I am going to use a comparison of two objects and return -1 (less than), 1 (greater than), 0 (equal)
    public delegate int Compare(object object1, object object2);
    If you create a basic method that will implement a delegate as

    Code:
    public static int StringCompare(object object1, object object2)
    {
    ....
    }
    This is just like any other method, but you can now attach the method that implements a compare

    To attach the "real" method implementation to the delegate you have to create a new instance of the delegate with the actual comparing method as a parameter e.g

    Code:
    Compare comp = new Compare(StringCompare)
    the comp is now linked to the StringCompare method and any call to the comp will just goto the StringCompare method instead, but of course if you had a IntegerCompare method just like the StringCompare you can re-link the comp (Compare) delegate to the IntegerCompare and the compiler does not matter as long as the IntegerCompare returns a integer value.

    Here is some example code in full

    Code:
    using System;
    
    namespace codecalldelegate
    {
    	/* setup the basics of the compare delegate method the returns are : 
    		 object1 > object2  = 1
    		 object1 < object2  = -1
    		 object1 == object2 = 0
    	*/
    	public delegate int Compare(object object1, object object2);
    	
    	class MainClass
    	{
    		public static int StringCompare(object object1, object object2)
    		{
    			string string1 = object1.ToString();
    			string string2 = object2.ToString();
    			
    			if (String.Compare(string1, string2) > 0)
    			{
    				return 1;
    			} else if (String.Compare(string1, string2) <0)
    			{
    				return -1;
    			}
    			else
    				return 0;
    		}
    
    		public static void Main(string[] args)
    		{
    			// setup a Compare object with the StringCompare method as the method to call
    			Compare comp = new Compare(StringCompare);
    			
    			Console.WriteLine("Compare Ian against Genux  : " + comp("Ian","Genux"));
    			Console.WriteLine("Compare Genux against Ian  : " + comp("Genux","Ian"));
    			Console.WriteLine("Compare Genux against Genux : " + comp("Genux", "Genux"));
    		}
    	}
    }
    and the output would be

    Code:
    Compare Ian against Genux  : 1
    Compare Genux against Ian  : -1
    Compare Genux against Genux : 0
    Delegates allow for methods to be assigned to different parts to be run at the run time, or you could multicast them which would allow for multiple methods called in order (for example if in a paint program and the user wants to have a circle, filled in, and moved to point x,y within a set macro you could have each action as a delegate multicasted.
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    genux's Avatar
    genux is offline Learning Programmer
    Join Date
    Dec 2009
    Location
    Norwich
    Posts
    79
    Rep Power
    8

    Re: Delegates

    Cool you had read my mind I was going to do a tutorial on events next
    Code:
    int coffeePerDay = 10; // need to cut down!!!
    Codingfriends

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Intermediate Tutorial on Delegates in managed C++
    By mnirahd in forum Tutorials
    Replies: 0
    Last Post: 11-21-2010, 08:23 AM
  2. Intermediate Tutorial on Delegates in managed C++
    By mnirahd in forum C Tutorials
    Replies: 0
    Last Post: 11-21-2010, 08:23 AM
  3. Delegates
    By zeelan in forum C# Programming
    Replies: 7
    Last Post: 07-13-2010, 04:21 AM
  4. Having trouble with Delegates...
    By vbstudent in forum Visual Basic Programming
    Replies: 8
    Last Post: 07-06-2010, 08:36 AM
  5. Problem handling delegates
    By rbk_achilles in forum C# Programming
    Replies: 1
    Last Post: 02-19-2008, 09:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts