0

私は次のコードを持っていますが、何らかの理由でエラーが発生しています:

タイプ 'System.StackOverflowException' の未処理の例外が WpfApplication1.exe で発生しましたat the line :this.JointName = joint; inside thepublic Customer(ストリングジョイント、ストリングアクション)

コードを実行すると。

Class1.cs コード:

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

namespace WpfApplication1
{
    public class Customer
    {
        public String JointName { get; set; }
        public String ActionName { get; set; }
        public List<Customer> lst = new List<Customer>();

        public Customer(String joint, String action)
        {
            this.JointName = joint;
            this.ActionName = action;
            this.lst.Add(new Customer(this.JointName, this.ActionName));
        }

        public List<Customer> GetList()
        {
            return this.lst;
        }
    }
}

MainWindow.xaml.cs コード:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Customer Data1 = new Customer("Joint1", "Action1");
            List<Customer> List = Data1.GetList();
            dataGrid1.ItemsSource = List;

        }
    }
}

ここで私が間違っていることがわかりますか?どこで無限ループできるかわかりません..

4

4 に答える 4

0

コンストラクターを繰り返しCustomer再帰的に呼び出しています。したがって、コール スタックは次のようになります。

Customer(String joint, String action)
Customer(String joint, String action)
Customer(String joint, String action)
Customer(String joint, String action)
Customer(String joint, String action)
...

list.Add各メソッドは完了後に呼び出すのを待っています。

setのプロパティへの呼び出しJointNameが、スタック フレームを壊す最後のメソッド呼び出しであることがたまたまです。

于 2013-02-25T19:20:32.517 に答える
0

Customer のコンストラクターには、最後の行に自身への呼び出しが含まれています。

this.lst.Add(new Customer(this.JointName, this.ActionName))

それが私たちのエラーの原因です。

于 2013-02-25T19:21:04.423 に答える
0

Customer のコンストラクターは常に "new Customer" を実行し、それが Customer のコンストラクターを呼び出し、以下同様に無限に続きます。

于 2013-02-25T19:21:08.893 に答える
0

Class1.cs

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

namespace WpfApplication1
{
    public class Customer
    {
        public String JointName { get; set; }
        public String ActionName { get; set; }


        public Customer(String joint, String action)
        {
            this.JointName = joint;
            this.ActionName = action;

        }


    }
}

MainWindow.xaml.cs コード:

 namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        /// 

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Customer Data1 = new Customer("Joint1", "Action1");
                List<Customer> list = new List<Customer>();
                list.add(Data1);
                dataGrid1.ItemsSource = list;

            }
        }
    }
于 2013-02-25T19:25:34.897 に答える