2014-09-26 28 views
-1

我得到这个错误在我的代码中,我试图让用户输入雇员的名字,他们赚了多少比收入通过所有的税收数学,然后给用户一个输出输入的姓名和拿回家的税款。我必须使用两个类。我哪里错了?请帮忙。Stackoverflowexception未处理。试图获取takehomepay价值

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; 
    } 
} 
} 

回答

0

试试这个:

public decimal takehomepay {get; set;} 
1

您setter和getter为takehomepay指的是自己。

要么跟着你的名字做同样的模式(有私有变量,然后使用的getter和setter)或只是这样做

public decimal takehomepay {get; set;} 
+0

我改变了你的建议,但现在我得到的错误“一个得到或设置acceutor期望” public decimal takehomepay {get;设置;) { 得到 { return takehomepay; } set { takehomepay = value; } } } – 2014-09-26 05:24:16

+0

@ nicko'connor删除最后一位,从字面上只是有'decimal takehomepay {get;设置;)'并且移除'{get {return takehomepay; } set {takehomepay = value; }}}' – 2014-09-26 05:26:37

+0

请注意,它应该''而不是'''在'set'之后'' – 2014-09-26 05:29:39

0

takehomepay二传手,你设置takehomepay了,所以当你尝试设置它时,它会自行调用,直到它崩溃。

public decimal takehomepay 
{ 
    set 
    { 
     takehomepay = value; 
    } 
} 
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