-1

I am having some issues with this app that i need to write for a c# class. I am getting an Unhandled Exception error. I know the problem relies within program.cs and TaxMath.cs, but I am unsure of where to go from here, any guidance would greatly be appreciated.

Error:

Unhandled Exception: System.FormatException: Index (zero based) must be greater
than or equal to zero and less than the size of the argument list.
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String fo
rmat, Object[] args)
   at System.String.Format(IFormatProvider provider, String format, Object[] arg
s)
   at System.IO.TextWriter.WriteLine(String format, Object arg0)
   at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0)
   at System.Console.WriteLine(String format, Object arg0)
   at TaxProgram.Program.Main(String[] args) in c:\Users\xxxx\Desktop\ConsoleApp
lication2\Program.cs:line 26

class Program
            {

                static void Main(string[] args)
                {
                    TaxValues tv = new TaxValues();

                    Console.WriteLine("Enter in your income: \r\n");

                    double income = Convert.ToDouble(Console.ReadLine());

                    Console.WriteLine("Enter in single, marryjointly, marryseperate, or      headohouse to enter in your filing status: \r\n");

                    FilingStatus fs = (FilingStatus)Enum.Parse(typeof(FilingStatus), Console.ReadLine());

    (line 24)       Tax TaxOwed = new Tax(tv, fs, income);

   (line 26)        Console.WriteLine("Your tax is: {1} \r\n ", TaxOwed.calculate());

class Tax
            {

                private TaxValues _taxvalues;
                private FilingStatus _filingstatus;
                double _income;
                
                public Tax (TaxValues tv, FilingStatus fs, double income)
                {
    (line 18)       Income = income;
                    _taxvalues = tv;
                    _filingstatus = fs;

                 }
                    public double Income
                    {
                        get { return _income; }
                        set { 
                            if (value <= 0)
                            {
                                throw new ArgumentException(String.Format("{0} must be > 0", value));
                            }       
                            _income = value;
    (line 32)               calculate();
                        }
                    }

                   private void calculate () 
                   {
                       double TaxOwed = 0.0;

                       if (_filingstatus == FilingStatus.single)
                       {
    (line 42)              if (Income <= _taxvalues.Single10)
                               TaxOwed = Income * .1;
                           else if (Income <= _taxvalues.Single15)
                               TaxOwed = Income * .15;
                           else if (Income <= _taxvalues.Single25)
                               TaxOwed = Income * .25;
                           else if (Income <= _taxvalues.Single28)
                               TaxOwed = Income * .28;
                           else if (Income <= _taxvalues.Single33)
                               TaxOwed = Income * .33;
                           else if (Income > _taxvalues.Single33)
                               TaxOwed = Income * .35;
                           else Console.WriteLine("You dun goofed");
                          .. 
                          return TaxOwed 0.0
donutvamp
  • 11
  • 3
  • Learn to read the details of the exception. It matters *which* exception, and *which line* caused it. Then isolate the code which causes it, and only post that code. Next, learn about arrays - your code will be much shorter. – Blorgbeard Oct 03 '14 at 01:57
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Blorgbeard Oct 03 '14 at 02:06
  • I don't have the knowledge to understand that general of an answer. – donutvamp Oct 03 '14 at 02:18
  • I pointed to the line numbers in the code tags. – donutvamp Oct 03 '14 at 02:24

1 Answers1

0

You perform calculation in your property:

    public double Income
    {
        get { return _income; }
        set
        {
            if (value <= 0)
            {
                throw new ArgumentException(String.Format("{0} must be > 0", value));
            }
            _income = value;
            //calculate(); <<ISSUE IS HERE
        }
    }

Change the signature of your method to return the double:

public double calculate()
{
  double TaxOwed = 0.0;
   ...
  return TaxOwed;
}

Finally log to the console:

Console.WriteLine("Your tax is: {0} \r\n ", TaxOwed.calculate());
meda
  • 45,103
  • 14
  • 92
  • 122