Demo entry 6632634

agafgadfadfadfhg

   

Submitted by afdsfadf on Jul 27, 2017 at 09:36
Language: C#. Code size: 1.5 kB.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DelegateEvent
{
    class Event2
    {
        public class Stock
        {
            string symbol;
            decimal price;

            // constructor
            public Stock(string symbol) { this.symbol = symbol; }

            public event EventHandler PriceChanged;

            protected virtual void OnPriceChanged(EventArgs e)
            {
                if (PriceChanged != null) PriceChanged(this, e);
            }

            public decimal Price
            {
                get { return price; }
                set
                {
                    if (price == value) return;
                    price = value;
                    OnPriceChanged(EventArgs.Empty);
                }
            }
        }

        
        public static void OnPriceChangedHandler(Object obj, EventArgs e)
        {
            Stock st = obj as Stock;
            if (st != null)
            {
                Console.WriteLine("price changed to: " + st.Price);
            }else
            {
                Console.WriteLine("asdasd");
            }
        }

        /*
        static void Main(String[] args)
        {
            Stock st = new Stock("Tuan") { Price = 0 };
            st.PriceChanged += OnPriceChangedHandler;
            st.Price = 10;
            Console.ReadKey();
        }
        */
    }
}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).