-1

I am writing a program that uses methods to take a user input for income and output what their income tax owed would be.

I have one error on line 46 which is ``private double GetTax(double taxable)` The GetTax is a CS0161 - not all code paths return a value.

Am I missing a reference to GetTax? Your input is much appreciated!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lab_4_Part_2_
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            //Set input and output to floating decimal
            double taxable = Convert.ToInt32(txtTaxable.Text);
            GetTax(taxable);

        }

        private void EnterNum(double taxable)
        {
            throw new NotImplementedException();
        }

        private double GetTax(double taxable)
        {


            if (taxable <= 9225) return (taxable * 1.1);
            if (taxable > 9225 | taxable < 37450) return (922.50 + (taxable * 1.15));
            if (taxable > 37450 | taxable < 90750) return (5156.25 + (taxable * 1.25));
            if (taxable > 90750 | taxable < 189300) return (18481.25 + (taxable * 1.28));
            if (taxable > 189300 | taxable < 411500) return (46075.25 + (taxable * 1.33));
            if (taxable > 411500 | taxable < 413200) return (119401.25 + taxable * 1.35);
            if (taxable > 413200) return (119996.25 + (taxable * 1.396));

        }


        public string DisplayTaxable(double taxable)
        {
            return  txtOwed.Text;
        }

    }
}
RJE
  • 21
  • 3
  • The GetTax method need to return double – Alexandre Neukirchen Oct 28 '17 at 04:11
  • You have multiple problems. Study the difference betwee OR and AND, to start. Then think about how the compiler sees your code, and what it might think about when all of your `return` statements are in `if`s. – Ken White Oct 28 '17 at 04:17
  • The error message means exactly what it says. In the `GetTax()` method, there is one or more code paths the method can take which don't return a value. Note that the compiler will not analyze your `if` conditions to see whether one can logically prove at least one of the `return` statements would be hit. In your case, you could just remove the last `if` statement and unconditionally execute `return (119996.25 + (taxable * 1.396));` See marked duplicates for various examples and solutions for this error. – Peter Duniho Oct 28 '17 at 04:24

1 Answers1

1

In your GetTax() method, all of your return statements are wrapped inside of if blocks. This means that, if none of the if blocks are hit, no value is currently being returned. Thus, the compilation error.

The simplest way to fix this would be to convert either your <= line or your > line into the final return statement line, for example:

private double GetTax(double taxable)
{
    if (taxable <= 9225) return (taxable * 1.1);
    //other cases omitted for brevity
    return (119996.25 + (taxable * 1.396));
}

You could use an else statement to preface that return value, but it isn't necessary and is more of a code style choice.

Chris Thompson
  • 490
  • 5
  • 17
  • There are more problems than what you've indicated in the code that would cause that error. For instance, look more carefully at `if (taxable > 9225 | taxable < 37450)`. – Ken White Oct 28 '17 at 04:15