Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Trying To Understand Delegates
Message
From
01/01/2008 16:44:43
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01278832
Message ID:
01278836
Views:
20
>I need help understanding delegates. I have read a number of articles, but it all seems like
>a lot of work to go through just to call a function.


Yes, in the example you gave, it does seem to be overkill. But, maybe thinking about it in terms of events and event handlers, it might make more sense to you. See if this helps at all. This is something I wrote up quite awhile ago ... I start out explaining how to set up events in a UserControl, but then get into defining custom delegates (which is what might interest you):

Say you have a custom UserControl that you need to have raise an event when, for example, a user types "FOO" in a textbox that is on the Control.

Minimally, in your UserControl, you need the following things:
// First you must make a public EventHandler:
public event EventHandler MyFooBar;

// Then, when you need to fire the event in your UserControl, do this:
if (this.MyTextBox.Text == "FOO")
	this.OnMyFooBar(new EventArgs());

// Lastly, this raises the MyFooBar event:
protected virtual void OnMyFooBar(EventArgs e)
{
	if MyFooBar != null)
		MyFooBar(this, e);
}
Then, in your form, you just set up the usual delegates and EventHandlers:
this.oMyControl.MyFooBar += new System.EventHandler(this.oMyControl_MyFooBarHandler);

private void oMyControl_MyFooBarHandler(object sender, System.EventArgs e)
{
	// whatever your code needs to be
}
You can even get more fancy, creating a custom delegate and custom EventArgs, but the above code is sufficient for simple things, when just the built-in System.EventArgs is all you need. For fancier, custom stuff, try this:

First, custom event args, something like this:
public class DoneEventArgs : EventArgs 
{ 
  private int m_MyEventArg;

  public int MyEventArg
  {
    get {return this.m_MyEventArg;}
    set {this.m_MyEventArg = value;}
  }
}
Next, a custom delegate:
public delegate void MyCustomEventHandler(object sender, DoneEventArgs e);
Everything else is pretty much the same, substituting stuff in the appropriate places:

The event handler in your UserControl then gets changed to this:
public event MyCustomEventHandler MyFooBar;
And in your Form, you'd have this instead:
this.oMyControl.MyFooBar += new MyCustomEventHandler(this.oMyControl_MyFooBarHandler);

private void oMyControl_MyFooBarHandler(object sender, DoneEventArgs e)
{
	// whatever your code needs to be
}
So, basically what is happening is that you're telling the UserControl that it should run the Form's oMyControl_MyFooBarHandler() method.

~~Bonnie






>
>See the lines markes in Main:
>
>
>using System;
>using System.Collections.Generic;
>using System.Text;
>
>public delegate string mydel(int number);
>
>class Program
>{
>    static void Main(string[] args)
>    {
>        MyClass obj1 = new MyClass(10);
>        MyClass obj2 = new MyClass(20);
>
>        mydel del1 = obj1.myfunc;   // I don't understand these 2 lines
>        mydel del2 = obj2.myfunc;
>
>        Console.WriteLine(del1(50));
>        Console.WriteLine(del2(20));
>    }
>}
>
>public class MyClass
>{
>    int x;
>    public MyClass(int x1)
>    {
>        x = x1;
>    }
>    public string myfunc(int n)
>    {
>        return (n * x).ToString();
>    }
>}
>
>
>
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform