-1

I'm getting this error in my code, i'm trying to make the user input the employees name and how much they earn than the earning goes through all the tax maths and then gives the user an output of the name entered and the take home pay - taxs. I have to use two classes. Where am I going wrong? Please help.

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 Answers4

1

You setter and getter for takehomepay is referring to itself.

Either follow the same pattern you do with the name (have a private variable and then use the getter and setters) or just do this

 public decimal takehomepay {get; set;}
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • I changed it to what you reccomended but now i'm getting the error "a get or set acceessor expected" public decimal takehomepay {get; set;) { get { return takehomepay; } set { takehomepay = value; } } } – nick o'connor Sep 26 '14 at 05:24
  • @nicko'connor Remove the last bit, literally just have `decimal takehomepay {get; set;)` and remove `{ get { return takehomepay; } set { takehomepay = value; } } }` – David Pilkington Sep 26 '14 at 05:26
  • Note that it should be `}`, not `)` after `set;` – Jon Skeet Sep 26 '14 at 05:29
  • Okay I see, I did that thank you. But now my takehomepay = 0. why isn't it using the maths? Wow, i'm so stuck on this :/ – nick o'connor Sep 26 '14 at 05:31
  • @nicko'connor now you have a two variables called `takehomepay` one local one and one in the class. Make sure that you are using the correct one – David Pilkington Sep 26 '14 at 05:40
  • @DavidPilkington I'm not sure which one it's using. I can't figure out what i'm doing wrong at the moment at all. I've exhausted myself on this question, it's taken me days. – nick o'connor Sep 26 '14 at 05:46
  • @nicko'connor in your `Main` method, you have a variable called `takehomepay` as well as in your `Employee` class. – David Pilkington Sep 26 '14 at 05:48
  • I want it to use the variable in my main method as that has the math behind it but I thought it was using that one, I tried taking the whole decimal takehomepay {get;set;) out of my employee class but that then made the takehomepay give me an error that the takehomepay is a type but is being used as a variable? Sorry, i'm just really stuck on this at the moment. I appreciate your help – nick o'connor Sep 26 '14 at 05:55
0

Try this instead:

public decimal takehomepay {get; set;}
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
0

In takehomepay setter, you're setting takehomepay again, so when you try to set it, it calls itself until it crashes.

public decimal takehomepay
{
    set
    {
        takehomepay = value;
    }
}
TheEvilPenguin
  • 5,634
  • 1
  • 26
  • 47
0

You've got a nasty recursive call here:

enter image description here

To solve this, try to adhere the convention of using PascalCase to property names.

private decimal takehomepay;

public decimal Takehomepay
{
    get
    {
        return takehomepay;
    }

    set
    {
        takehomepay = value;
    }
}

Also, to understand why your StackOverflowException was unhandled, see C# catch a stack overflow exception.

Community
  • 1
  • 1
sampathsris
  • 21,564
  • 12
  • 71
  • 98