二传手一直自称递归

问题描述 投票:0回答:1

我练的C#事件和委托。当我运行代码,我获得过程被终止由于StackOverflowException,因为二传手是递归调用本身由于线路:

CurrentPrice = value; //inside the setter of CurrentPrice

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

namespace PriceEvent
{

    public class PriceChangingEvent : EventArgs
    {

        public int Counter = 0; 

        public int CurrentPrice {

            get
            {
                return CurrentPrice; 

            }

            set
            {
                // only invoke the Event when the Class PriceChangingEvent has been instantiated
                if (Counter > 0) // that is when we are not using the constructor
                {
                    CallPriceChanger(this);
                }

                CurrentPrice = value;
                ++Counter; 

            }

        }
        public int NewPrice { get; set; }

        // 2 args Constructor , constructor invokes setter 
        public PriceChangingEvent(int currentprice, int newprice)
        {
            this.CurrentPrice = currentprice;  // invokes the setter of CurrentPrice
            this.NewPrice = newprice;

        }


         //1. define a delegate between publisher and subscribers
         //source publisher who triggers the event,  
        public delegate void CurrentPriceChangeEventHandler(object source, PriceChangingEvent PriceEvent);

        // 2. define an event
        public event CurrentPriceChangeEventHandler PriceChange;

        // 3. raise the event, OnDataTrained is the method which calls the delegate
        protected virtual void OnCurrentPriceChange(PriceChangingEvent PriceEvent)
        {
           PriceChange.Invoke(this, PriceEvent);
        }

        // 3.Function which raises the event, OnPriceChanger is the method which calls the delegate
        protected virtual void OnPriceChanger(PriceChangingEvent PriceChangingEvent)
        {
            // this: the class 
            PriceChange.Invoke(this, PriceChangingEvent);
        }


        // Function to call the function OnPriceChanger
        public void CallPriceChanger(PriceChangingEvent PriceChangingEvent)
        {

            OnPriceChanger(PriceChangingEvent);

        }

    }

    class Program
    {
        static void Main(string[] args)
        {

            PriceChangingEvent p = new PriceChangingEvent(20, 30);

            p.CurrentPrice = 45;



        }

        //subscribers

        public static void Display(PriceChangingEvent p)
        {
            Console.WriteLine("Current Price has been changed to {0}", p.CurrentPrice);

        }
    }
}
c# .net
1个回答
4
投票

当你的代码比最小的getter或setter更多,你需要有属性支持字段:

private int _currentPrice;

public int CurrentPrice
{
    get
    {
        return _currentPrice; 

    }
    set
    {
        if (Counter > 0) // that is when we are not using the constructor
        {
            CallPriceChanger(this);
        }

        _currentPrice = value;
        ++Counter; 
    }
}

您可以进一步使用支持字段来简化代码,其中的构造现在可以直接设置支持字段,你不再需要Counter价值可言。

public class PriceChangingEvent : EventArgs
{
    public PriceChangingEvent(int currentprice, int newprice)
    {
        _currentPrice = currentprice;  
        NewPrice = newprice;
    }

    private int _currentPrice;
    public int CurrentPrice
    {
        get
        {
            return _currentPrice; 
        }
        set
        {
            CallPriceChanger(this);
            _currentPrice = value;
        }
    }

    //...
}
© www.soinside.com 2019 - 2024. All rights reserved.