Stackoverflowexception未处理。试图获得takehomepay价值

时间:2014-09-26 05:16:53

标签: c# class

我在我的代码中收到此错误,我试图让用户输入员工姓名以及他们获得的收入超过所有税收数学的收入,然后为用户提供输出输入的名称和带回家的工资税。我必须使用两个班级。我哪里错了?请帮忙。

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

namespace consoleapplication9
{
public class takehomepay
{
    static void Main(String[] args)
    {
        const decimal commission = 0.7M; // Commision rate
        const decimal federaltax = 0.18M; // federal tax rate
        const decimal retirement = 0.10M; // retirement rate
        const decimal socialsecurity = 0.06M; // social security rate

        string employeeName;
        decimal commcost = 0; // commision cost
        decimal fedtaxcost = 0; // federal tax cost
        decimal retirecost = 0; // retirement cost
        decimal socseccost = 0; // social security cost
        decimal totalwithholdingcost = 0; // total withholding
        decimal takehomepay = 0; // amount taken home
        decimal totalSales = 0;

        Console.Write("\nEnter employees name: ");
        employeeName = Console.ReadLine();

        Console.Write("Enter the total sales amount for the week:");
        totalSales = Convert.ToDecimal(Console.ReadLine());

        var employee = new Employee(employeeName, totalSales);
        Console.Write(employee);
        Console.Read();

        //Calculations
        commcost = commission * totalSales;
        fedtaxcost = federaltax * commcost;
        retirecost = retirement * commcost;
        socseccost = socialsecurity * commcost;
        totalwithholdingcost = federaltax + retirement + socialsecurity;
        takehomepay = commcost - totalwithholdingcost;
    }
}

public class Employee
{
    private string employeeName;
    private decimal totalSales;
    public Employee()
    {
    }
    public Employee(string Name)
    {
        employeeName = Name;
    }
    public Employee(string Name, decimal Sales)
    {
        employeeName = Name;
        totalSales = Sales;
    }
    public string EmployeeName
    {
        get
        {
            return employeeName;
        }
        set
        {
            employeeName = value;
        }

    }

    public decimal takehomepay
    {

        get
        {
            return takehomepay;
        }

        set
        {
            takehomepay = value;
        }
    }
    public override string ToString()
    {
           return "Employee: " + employeeName +
            "\nTake home pay: " + takehomepay;
    }
}
}

4 个答案:

答案 0 :(得分:1)

takehomepay的setter和getter指的是它自己。

使用与名称相同的模式(拥有私有变量,然后使用getter和setter)或者只是执行此操作

 public decimal takehomepay {get; set;}

答案 1 :(得分:0)

请改为尝试:

public decimal takehomepay {get; set;}

答案 2 :(得分:0)

takehomepay设置器中,您再次设置takehomepay,因此当您尝试设置它时,它会自行调用直至崩溃。

public decimal takehomepay
{
    set
    {
        takehomepay = value;
    }
}

答案 3 :(得分:0)

你这里有一个令人讨厌的递归电话:

enter image description here

要解决此问题,请尝试遵守使用PascalCase到属性名称的惯例。

private decimal takehomepay;

public decimal Takehomepay
{
    get
    {
        return takehomepay;
    }

    set
    {
        takehomepay = value;
    }
}

另外,要了解StackOverflowException 未处理 的原因,请参阅C# catch a stack overflow exception