4 Replies - 530 Views - Last Post: 04 March 2012 - 11:59 PM Rate Topic: -----

#1 hocnv_vn  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 09-January 12

question: what do "event" keyword do in this case?

Posted 03 March 2012 - 08:53 PM

i get this code from MSDN help. What different from original and edited?
namespace DotNetEvents
{
    using System;
    using System.Collections.Generic;

    // Define a class to hold custom event info
    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            message = s;
        }
        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }

    // Class that publishes an event
    class Publisher
    {

        // Declare the event using EventHandler<T>
        public event EventHandler<CustomEventArgs> RaiseCustomEvent;

        public void DoSomething()
        {
            // Write some code that does something useful here
            // then raise the event. You can also raise an event
            // before you execute a block of code.
            OnRaiseCustomEvent(new CustomEventArgs("Did something"));

        }

        // Wrap event invocations inside a protected virtual method
        // to allow derived classes to override the event invocation behavior
        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            // Event will be null if there are no subscribers
            if (handler != null)
            {
                // Format the string to send inside the CustomEventArgs parameter
                e.Message += String.Format(" at {0}", DateTime.Now.ToString());

                // Use the () operator to raise the event.
                handler(this, e);
            }
        }
    }

    //Class that subscribes to an event
    class Subscriber
    {
        private string id;
        public Subscriber(string ID, Publisher pub)
        {
            id = ID;
            // Subscribe to the event using C# 2.0 syntax
            pub.RaiseCustomEvent += HandleCustomEvent;
        }

        // Define what actions to take when the event is raised.
        void HandleCustomEvent(object sender, CustomEventArgs e)
        {
            Console.WriteLine(id + " received this message: {0}", e.Message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Publisher pub = new Publisher();
            Subscriber sub1 = new Subscriber("sub1", pub);
            Subscriber sub2 = new Subscriber("sub2", pub);

            // Call the method that raises the event.
            pub.DoSomething();

            // Keep the console window open
            Console.WriteLine("Press Enter to close this window.");
            Console.ReadLine();

        }
    }
}




namespace DotNetEvents
{
    using System;
    using System.Collections.Generic;

    // Define a class to hold custom event info
    public class CustomEventArgs : EventArgs
    {
        public CustomEventArgs(string s)
        {
            message = s;
        }
        private string message;

        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }

    // Class that publishes an event
    class Publisher
    {

        // Declare the event using EventHandler<T>
        public [color="#FF0000"][size="5"][i][b]event[/b][/i][/size][/color] EventHandler<CustomEventArgs> RaiseCustomEvent;

        public void DoSomething()
        {
            // Write some code that does something useful here
            // then raise the event. You can also raise an event
            // before you execute a block of code.
            OnRaiseCustomEvent(new CustomEventArgs("Did something"));

        }

        // Wrap event invocations inside a protected virtual method
        // to allow derived classes to override the event invocation behavior
        protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
        {
            // Make a temporary copy of the event to avoid possibility of
            // a race condition if the last subscriber unsubscribes
            // immediately after the null check and before the event is raised.
            EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

            // Event will be null if there are no subscribers
            if (handler != null)
            {
                // Format the string to send inside the CustomEventArgs parameter
                e.Message += String.Format(" at {0}", DateTime.Now.ToString());

                // Use the () operator to raise the event.
                handler(this, e);
            }
        }
    }

    //Class that subscribes to an event
    class Subscriber
    {
        private string id;
        public Subscriber(string ID, Publisher pub)
        {
            id = ID;
            // Subscribe to the event using C# 2.0 syntax
            pub.RaiseCustomEvent += HandleCustomEvent;
        }

        // Define what actions to take when the event is raised.
        void HandleCustomEvent(object sender, CustomEventArgs e)
        {
            Console.WriteLine(id + " received this message: {0}", e.Message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Publisher pub = new Publisher();
            Subscriber sub1 = new Subscriber("sub1", pub);
            Subscriber sub2 = new Subscriber("sub2", pub);

            // Call the method that raises the event.
            pub.DoSomething();

            // Keep the console window open
            Console.WriteLine("Press Enter to close this window.");
            Console.ReadLine();

        }
    }
}


it run as same. :| :withstupid:

This post has been edited by tlhIn`toq: 04 March 2012 - 08:05 AM
Reason for edit:: Just select the code section then hit the [code] button to place the proper tags.


Is This A Good Question/Topic? 0
  • +

Replies To: question: what do "event" keyword do in this case?

#2 CreaturGames  Icon User is offline

  • D.I.C Regular

Reputation: 30
  • View blog
  • Posts: 290
  • Joined: 29-December 09

Re: question: what do "event" keyword do in this case?

Posted 03 March 2012 - 09:41 PM

Here are some references to explain what you are asking:

http://msdn.microsof...a(v=vs.71).aspx

http://msdn.microsof...9(v=vs.71).aspx

Hope this helps.

EDIT: Also, there are real-time examples I could give to you:

You make a chocolate cake. This is the event. The actions, or methods, that could be used vary, such as eating the cake or storing it.

In this case, the code given states that if the event, or the custom event, occurs within the code, then carry one of the methods out, such as Console.WriteLine(id + " received this message: {0}", e.Message);.

Here is a really simple example:

this.Load += new System.EventHandler(this.Form1_Load);



In this code, there is a new event declared, or called, and a new Event is checked for in the application, which in this instance is the Load event handler.

This post has been edited by CreaturGames: 03 March 2012 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 hocnv_vn  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 09-January 12

Re: question: what do "event" keyword do in this case?

Posted 03 March 2012 - 09:47 PM

i'm sorry. Some mistake are here. In first code, i used "event" key word but second not used. The result is not different. can we ignore "event"?
Was This Post Helpful? 0
  • +
  • -

#4 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 992
  • View blog
  • Posts: 972
  • Joined: 30-September 10

Re: question: what do "event" keyword do in this case?

Posted 04 March 2012 - 03:51 AM

Quote

The result is not different. can we ignore "event"?


Definitely not. You should always use the event keyword when creating events. See this thread for what the event keyword does, and why it is important.
Was This Post Helpful? 2
  • +
  • -

#5 hocnv_vn  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 15
  • Joined: 09-January 12

Re: question: what do "event" keyword do in this case?

Posted 04 March 2012 - 11:59 PM

it's clearly enough to me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1