2014-09-26 66 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