Loading ...

Delegates and Events.

Who is online?  0 guests and 1 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Delegates and Events.

Delegates and Events.

Posts under the topic: Delegates and Events.

Posted: 7/15/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

Today I have learned what is delegate and events. So here I am just checking myself by this example.

Example -

using System;

public delegate void DelegateUniqueNumber(int Number);

class GetUniqueNumber
{
    public event DelegateUniqueNumber UniqueNumber;
    public GetUniqueNumber()
    {
        UniqueNumber = null;
    }
    
    public void GenrateUniqueNumber()
    {
        // object of an Array class.
        int[] arr = new int[10];
        bool flag;
        Random random = new Random();

        for (int i = 0; i < 10; i++)
        {
            int randomNo = random.Next(1, 20);
            flag = true;
           
            for (int x = 0; x < arr.Length; x++)
            {
                if (randomNo == arr[x])
                {

                    i--;
                    flag = false;
                    break;
                }
            }

            if (flag)
            {
                arr[i] = randomNo;
                UniqueNumber(randomNo);
            }
        }
        
    }
}
class EventHandlerClass
{
    public void PrintUniqueNumbers(int number)
    {
        Console.WriteLine(number);
    }
}
class MyMain
{
    public static void Main()
    {
        GetUniqueNumber MyNumb = new GetUniqueNumber();
        EventHandlerClass MyEvenNumberHandlerClass = new EventHandlerClass();
        MyNumb.UniqueNumber += new DelegateUniqueNumber(MyEvenNumberHandlerClass.PrintUniqueNumbers);
        MyNumb.GenrateUniqueNumber();

        Console.ReadLine();
    }
}


In above examle I am getting unique number. Whenever I get a unique number, I will fire an event and print that value.So in above example I am using delegate and event.

Hajan Can you please tell me my example is rite?


Posted: 7/15/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

Yes :).

You have used it correctly. I didn't check in details what your GenrateUniqueNumber method actually does, but the concept you use is on the correct way.

Moreover, I would recommend you to try learning how to build components in .NET - this will definitely make you learn more advanced concepts of using delegates and events including multicasting, lambda expressions, anonymous methods and so on.

Delegates and Events play the main role especially when building components! ;)

Regards,
Hajan


Posted: 7/15/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

hajan said:

Yes :).

You have used it correctly. I didn't check in details what your GenrateUniqueNumber method actually does, but the concept you use is on the correct way.

Moreover, I would recommend you to try learning how to build components in .NET - this will definitely make you learn more advanced concepts of using delegates and events including multicasting, lambda expressions, anonymous methods and so on.

Delegates and Events play the main role especially when building components! ;)

Regards,
Hajan

Thank you Hajan:)

Yes I will do and let you know if I have any confusionWink

 

 


Posted: 7/15/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

Feel free to ask always anything you want :) I will be glad to answer the best way I can.


Posted: 7/15/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

hajan said:

Feel free to ask always anything you want :) I will be glad to answer the best way I can.

Smile


Posted: 7/13/2011

Lurker 25  points  Lurker
  • Joined on: 7/13/2011
  • Posts: 1
  Answered

Events :

The Event model in C# finds its roots in the event programming model that is popular in asynchronous programming. The basic foundation behind this programming model is the idea of "publisher and subscribers." In this model, you have publishers who will do some logic and publish an "event." Publishers will then send out their event only to subscribers who have subscribed to receive the specific event.

 

Delegates :

Delegates are very similar to 'pointers to functions' in C and C++

First we create a delegate, then we can assign any function that has a similar prototype of the one which we have specified with the delegate.

Declaration:

public delegate int Dlg(int x, int y); //where Dlg is the name of Delegate

Create Object:
when we need to create a 'Delegate object' (i.e. a pointer to a function which has a specified prototype)

Dlg d1=new Dlg(function_name); //if the class is in some other class use 'class_name.function_name'

function_name->has to be the name of a function which receives two integers as arguments and return an integer

Invoking function:

int res=d1(20, 20);

Cegonsoft


Page 1 of 1 (6 items)