Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Stackoverflowexception未处理。试着让takehomepay物有所值_C#_Class - Fatal编程技术网

C# Stackoverflowexception未处理。试着让takehomepay物有所值

C# Stackoverflowexception未处理。试着让takehomepay物有所值,c#,class,C#,Class,我在我的代码中得到了这个错误,我试图让用户输入员工的姓名,以及他们的收入超过收入的多少,通过所有的税务计算,然后给用户一个输入姓名和带回家的工资税的输出。我必须使用两个类。我哪里做错了?请帮忙 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace consoleapplication9 { publ

我在我的代码中得到了这个错误,我试图让用户输入员工的姓名,以及他们的收入超过收入的多少,通过所有的税务计算,然后给用户一个输入姓名和带回家的工资税的输出。我必须使用两个类。我哪里做错了?请帮忙

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;
    }
}
}
请尝试以下方法:

public decimal takehomepay {get; set;}

您认为,
takehomepay
的setter和getter指的是它自己

要么遵循与名称相同的模式(使用私有变量,然后使用getter和setter),要么就是这样做

 public decimal takehomepay {get; set;}

takehomepay
setter中,您再次设置了
takehomepay
,因此当您尝试设置它时,它会调用自己,直到崩溃

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

这里有一个讨厌的递归调用:

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

private decimal takehomepay;

public decimal Takehomepay
{
    get
    {
        return takehomepay;
    }

    set
    {
        takehomepay = value;
    }
}

另外,要理解为什么您的
StackOverflowException
未经处理,请参阅。

我将其更改为您建议的,但现在我收到错误“get或set acceessor expected”公共十进制takehomepay{get;set;){get return takehomepay;}设置{takehomepay=value;}}}@nicko'connor删除最后一位,字面上只需使用
decimal takehomepay{get;set;)
并删除
{get{return takehomepay;}设置{takehomepay=value;}}
注意它应该是
,,不
设置之后takehomepay
一个是本地变量,另一个是类中变量。确保您使用的是正确的