Feeds:
Posts
Comments

Posts Tagged ‘Delegate’

Introduction:

In a WinForm application we can use as much encapsulated UserControls as we want. But what would happen if one of these UserControl must communicate with another one?

Here we will have a look on 2 way to give our solution :

  1. Using Parent property of a control
  2. Using delegates and event programming

The 2nd way is much better because more portable, readable and maintainable!

In this exemple a WinForm will contain a usercontrol (UserControl1) which contains another usercontrol (UserControl2). UserControl2 contains a comboBox having a collection of colors. Choosing a color in this comboBox will change the color of the WinForm control.

Using Parent property of a control

public partial class UserControl2 : UserControl
{
    private void comboBox1_SelectedIndexChanged(object s, EventArgs e)
    {
       #region Using Parent class (bad way)
       switch (this.comboBox1.SelectedIndex)
       {
            case 0:
              //1rst Parent is GroupBox
              //2nd Parent is UserControl1
              //3rd Parent is Form
              Parent.Parent.Parent.BackColor = Color.Blue;
            break;
            case 1:
              Parent.Parent.Parent.BackColor = Color.Red;
            break;
        }
       #endregion
}

Using delegates and event programming

Declaration of our delegate, event and method caling the event

public delegate void ColorChanger(Color color);
public partial class UserControl2 : UserControl
{
    public event ColorChanger ColorChangerExecuter;
    public UserControl2()
    {
            InitializeComponent();
    }

    public void OnChangedSelectIndex(Color color)
    {
        if (this.ColorChangerExecuter != null)
            this.ColorChangerExecuter(color);
    }

The other way of implementing comboBox1_SelectedIndexChanged method

private void comboBox1_SelectedIndexChanged(object s, EventArgs e)
{
    #region Using Delegate with Event (better way)
    switch (this.comboBox1.SelectedIndex)
    {
        case 0:
               this.OnChangedSelectIndex(Color.Blue);
               break;
        case 1:
               this.OnChangedSelectIndex(Color.Red);
               break;
    }
    #endregion

And the call in the WinForm class

public partial class Form1 : Form
{
   UserControl2 uc2 = new UserControl2();
   public Form1()
   {
       InitializeComponent();
       uc2 = this.userControl11.userControl2;
       uc2.ColorChangerExecuter+=new
                            ColorChanger(uc2_ColorChangerExecuter);
  }

 private void uc2_ColorChangerExecuter(Color color)
 {
     this.BackColor = color;
 }
}

A listener has been created, and whatever happen to the comboBox, as we have implemented an event for this control, it will be automatically passed to the WinForm container.

Read Full Post »

Generic delegate Func

Here’s a new feature introduced in C#3.0 :  Func
Func is a new type used in all concerning delegate declarations. In this post, we will show you the different states in using delagates declaration between C#1.1, 2.0 and 3.5.
The following examples show if a data is included in a given interval.  In C#1.0 the code was like this :

Ιrst Step : In this case, we were declaring delegate IsInRange targetting testedData method  having 3 parameters and returning a Boolean.

public delegate bool IsInRange<T>(T testedData, T minBound,
T maxBound);
public class GenericDelegate
{
  public T minBound<T>(T param)
  {
   return param;
  }
  public T maxBound<T>(T param)
  {
   return param;
  }
}
ΙΙ Step : We were implementing our targetted method as shown below.
public bool testedData<T>(T dataToTest, T minBound, T maxBound)
where T : IComparable
{
bool toReturn = false;
if((dataToTest.CompareTo(minBound)<0||dataToTest.CompareTo(maxBound) > 0))
{return toReturn;}
if((dataToTest.CompareTo(minBound)==0&&dataToTest.CompareTo(maxBound)== 0) ||
(dataToTest.CompareTo(minBound) > 0 && dataToTest.CompareTo(maxBound) < 0))
{toReturn = true;}
return toReturn;
Method call in C#1.1, here we invoke the targetted method like what is shown below
GenericDelegate genericDelegate = new GenericDelegate();
//C#1.1
IsInRange<int> isInRange = new IsInRange<int>(genericDelegate.testedData);

Method call in C#2.0, note the anonymous method call with delegate

//C#2.0
IsInRange<int> isInRange = delegate(int testedData, int minBound, int maxBound)
{ bool toReturn = false;
if ((testedData.CompareTo(minBound) < 0 || testedData.CompareTo(maxBound) > 0))
{return toReturn;}
if ((testedData.CompareTo(minBound) == 0 && testedData.CompareTo(maxBound) == 0) ||
(testedData.CompareTo(minBound) > 0 && testedData.CompareTo(maxBound) < 0))
{toReturn = true;}
return toReturn;
};

//Method call  in C#3.0 with Func type
//C#3.5
//First way
Func<int, int, int, bool> isInRange = delegate(int testedData, int minBound,
int maxBound)
{bool toReturn = false;
if ((testedData.CompareTo(minBound) < 0 || testedData.CompareTo(maxBound) > 0))
{return toReturn;
if ((testedData.CompareTo(minBound) == 0 && testedData.CompareTo(maxBound) == 0) ||
(testedData.CompareTo(minBound) > 0 && testedData.CompareTo(maxBound) < 0))
{toReturn = true;}
return true;
};

Δ NOTE In only one line you have the delegate declaration including the types (int)of the target method parameters and //the type (bool) of //the return of this method. Note the lambda expression used to reduce this code.

//Second Way
Func<int, int, int, bool> isInRange =
(int data, int min, int max) => (data.CompareTo(min) < 0 ||
 data.CompareTo(max) > 0)?false:true;
 bool toReturn = isInRange(3, 2, 1);

Read Full Post »