“错误消息:System.StackOverflowException,如何修复System.StackOverflowException?

时间:2019-07-27 00:54:43

标签: c#

RetailItem类编写一个名为Retai1Item的类,该类保存有关零售商店中商品的数据。该类应具有以下属性:Description-Description属性应保留对UnitsOnHand项的简短描述-UnitsOnHand属性应保留当前库存中的单元数。价格-价格属性应包含商品的零售价。编写一个接受每个属性参数的构造函数该应用程序应创建一个三个Retailltem对象的数组,其中包含以下数据:描述现货单位价格夹克项目1 12 59.95项目2 40牛仔裤34.95 20项目3衬衫24.95

Code:

Retail Class:



    using System;
       using System.Collections.Generic;
       using System.Linq;
       using System.Text;
       using System.Threading.Tasks;
       using System.IO;
       namespace RetailApp
       {
       class RetailItems
       {

       private string Description;
       private int UnitsOnHand;
       private decimal Price;

       public RetailItems( string description, int unitsonhand, decimal price )
       {
       Description = description;
       UnitsOnHand = unitsonhand;
       Price = price;

       }
       public string description //ERROR
       get
       {
       return description;//ERROR
       }


       }
       public int unitsonhand
       {
       get
       {
       return unitsonhand;
       }
       set
       {
       if (value < 0)
       {
       throw new Exception(" UnitsOnHand must be >0");
       }
       else
       unitsonhand = value;
       }
       }
       public int price
    {
    get
    {
    return price;
    }
    set
    {
    if (value < 0)
    {
    throw new Exception(" Price must be > 0");
    }
    else
    price = value;
    }
    }
    public override string ToString()
    {
    return string.Format("{0} {1}; {2};{3:c)", Description, UnitsOnHand, Price); }

    }

    }
    Form1:

    using System;
    using System.Windows.Forms;

    namespace RetailApp
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    RetailItems[] myitems = new RetailItems[3];
    myitems[0] = new RetailItems("Jacket", 12, 59.95m);
    myitems[1] = new RetailItems("Jeans", 40, 34.95m);
    myitems[2] = new RetailItems("Shirt", 20, 24.95m);

    for (int i = 0; i < myitems.Length; i++)
    {
    listBox1.Items.Add("ITEM" + (i + 1) + "\t" + myitems[i].description + "\t" + myitems[i].unitsonhand + "\t\t" + myitems[i].price.ToString("f2"));
    }
    }
    }
    }

错误消息:System.StackOverflowException->消息=引发了类型为'System.StackOverflowException'的异常。

0 个答案:

没有答案