简体   繁体   English

输入名称时,我的代码将返回StackOverFlowException ...我不明白为什么

[英]My code returns a StackOverFlowException when I enter a name… I don't understand why

My code's purpose is to change my 'dispatcher' object's name and whilst this is done through the dispatcher's name property's setter, it runs OnNameChange() where I raise the event. 我的代码的目的是更改“调度程序”对象的名称,尽管这是通过调度程序的name属性的设置器完成的,但它运行OnNameChange()引发事件。

This is where I want the "OnDispatcherNameChange" method to be run where it displays a message ("Dispatcher's name changed to "). 这是我要在其中显示消息的地方运行“ OnDispatcherNameChange”方法的地方(“ Dispatcher的名称更改为”)。

However, when I run the main, and enter a name, it returns a StackOverFlowException. 但是,当我运行main并输入名称时,它将返回StackOverFlowException。 How can I fix this? 我怎样才能解决这个问题?

This is my code: 这是我的代码:

using System;

namespace Excercise_Events
{

    public class NameChangeEventArgs : EventArgs
    {
        public string Name { get; private set; }

        public NameChangeEventArgs(string name)
        {
            this.Name = name;
        }
    }
    public class Dispatcher
    {
        public string Name
        {
            get
            {
                return Name; 
            }
            set
            {
                var nameChange = new NameChangeEventArgs(Name);
                OnNameChange(nameChange);

                Name = value; 
            } 
        }

        public delegate void NameChangeEventHandler(object Source, NameChangeEventArgs args);

        public event NameChangeEventHandler NameChange;

        protected virtual void OnNameChange(NameChangeEventArgs args)
        {
            NameChange?.Invoke(this, new NameChangeEventArgs(args.Name));
        }

    }

    public class Handler
    {
        public void OnDispatcherNameChange(object Source, NameChangeEventArgs args)
        {
            Console.WriteLine("Dispatcher's name changed to {0}", args.Name);
        } 
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dispatcher = new Dispatcher();
            var handler = new Handler();

            dispatcher.NameChange += handler.OnDispatcherNameChange;

            while (true)
            {
                Console.Write("Enter new Name: ");
                string name = Console.ReadLine();
                if (name.ToLower() == "end")
                {
                    break; 
                }
                dispatcher.Name = name;
            }
         }
    }
}

The Name property is the problem. 名称属性是问题。 You have an infinite loop where the setter is calling the property again to set another value. 您有一个无限循环,在该循环中,setter再次调用该属性以设置另一个值。

Change it to use a backing field: 更改它以使用后备字段:

private string _name;

public string Name
{
    get
    {
        return _name; 
    }
    set
    {
        var nameChange = new NameChangeEventArgs(Name);
        OnNameChange(nameChange);

        _name = value; 
     } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不明白为什么此代码有效? - I don't understand why this code works? 我的代码在执行时抛出 stackoverflowexception - My code throws stackoverflowexception when i execute 控制器请求返回状态码 401,我不明白为什么,虽然我写下了我需要的一切 - The controller request returns a status code of 401, I don't understand why, though I wrote down everything I needed 我不明白为什么我的排序不起作用 - I don't understand why my sort isn't working 我的应用程序崩溃与FileNotFoundException,我不明白为什么 - My application crashes with a FileNotFoundException, and I don't understand why 我需要帮助,因为我不明白为什么我的代码没有按日期执行过滤? ASP.NET - I need help because I don't understand why my code is not performing filtering by date? ASP.NET MissingMethodException但我不明白为什么 - MissingMethodException but I don't understand why 出于某种原因,我的代码不应该在我的僵尸中产生,我不知道为什么 - For some reason my code is spawning in my zombies when it is not supposed to and I don't know why 如何在我的代码中防止StackOverflowException - How can I prevent StackOverflowException in my code 为什么我的媒体资源会收到StackOverflowException? - Why do I get a StackOverflowException with my property?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM