Calling method from another method

jcs

Member
Joined
Jun 23, 2012
Messages
7
Programming Experience
Beginner
The application I am creating is a form that will take a salary that is typed into a text box and calculates the annual amount of income tax. My problem is that I cannot make calculate_Master method to work, so right now I just have the calculate_Single executing when the calculate button is clicked. I would like to make it so that the calculate_Master will call the appropriate method depending on what value the person chooses in the dropdown box. (Single, Married Filing Jointly, or Head of Household)

In this example, the resulting error is:
"Only assignment, call, increment, decrement, and new object expressions can be used as a statement"

Side note: I am in the process of trying to teach myself using online materials and admittedly, I lack some basics that I am trying to catch up on. That being said, I am running up against two issues that I would like help on. If, while you are reviewing this code, notice anything I need to change for conventional practices or any reason, I would appreciate any such advice.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaxCalculator
{
    public partial class Form1 : Form
    {

        string salarynocomma;
        string salarycomma; 
        int salaryint;


        int singleA = 8700;
        int singleB = 35350;
        int singleC = 85650;
        int singleD = 178650;
        int singleE = 388350;
        int jointA = 17400;
        int jointB = 70700;
        int jointC = 142700;
        int jointD = 217450;
        int jointE = 388350;
        int headA = 12400;
        int headB = 47350;
        int headC = 122300;
        int headD = 198050;
        int headE = 388350;

        double bracketArate = .10;
        double bracketBrate = .15;
        double bracketCrate = .25;
        double bracketDrate = .28;
        double bracketErate = .33;
        double bracketFrate = .35;
        double taxresult;
        public Form1()
        {
            InitializeComponent();
        }

        
        private void testSalaryInput_Click(object sender, EventArgs e)
        {

        }

        private void calculate_Master(object sender, EventArgs e)
        {

          
            MessageBox.Show("You entered " + salarycomma + ".");

            if (filingStatusDropdown.Text == "Single")
            {
                calculate_Single;
                
                taxResultLabel.Text = taxresult.ToString();
               
            }
            else if(filingStatusDropdown.Text == "Married Filing Jointly")
            {
                calculate_Joint;
                taxResultLabel.Text = taxresult.ToString();
            }
            else if (filingStatusDropdown.Text == "Head of Household")
            {
                calculate_Head;
                taxResultLabel.Text = taxresult.ToString();
            }
            else 
            {
                MessageBox.Show("Please select a valid filing status.");
            }
            taxResultLabel.Text = taxresult.ToString();
            
        }

        private void calculate_Single(object taxresult, EventArgs e)
        {
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);
            
            //Calculate using Single Filing Status tax brackets

            if (salaryint < 0){MessageBox.Show("What is wrong with you?  Put in a number above 0.");}
            else if (salaryint <= singleA) {taxresult = salaryint * bracketArate;}
            else if (salaryint <= singleB) {taxresult = salaryint * bracketBrate;}
            else if (salaryint <= singleC) {taxresult = salaryint * bracketCrate;}
            else if (salaryint <= singleD) {taxresult = salaryint * bracketDrate;}
            else if (salaryint <= singleE) {taxresult = salaryint * bracketErate;}
            else if (salaryint > singleE) {taxresult = salaryint * bracketFrate;}
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
            
            taxResultLabel.Text = taxresult.ToString();
        }

        private void calculate_Joint(object sender, EventArgs e)
        {
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Joint Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= jointA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= jointB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= jointC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= jointD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= jointE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > jointE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }

            taxResultLabel.Text = taxresult.ToString();
            
        }

        private void calculate_Head(object sender, EventArgs e)
        {
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Head of Household Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= headA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= headB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= headC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= headD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= headE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > headE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }

            taxResultLabel.Text = taxresult.ToString();
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
       
    }
}
 
Oh sorry, inside the calculate_Master method, there are three lines:

calculate_Single;
calculate_Joint;
calculate_Head;

Each one of those generate the same error message.
 
If I add () to each of those lines, then I get these error messages for those methods:
No overload for method 'calculate_Single' takes 0 arguments
No overload for method 'calculate_Joint' takes 0 arguments
No overload for method 'calculate_Head' takes 0 arguments
 
It is telling you that you need to pass in the correct parameters for each method.

With the way your calculate_Single is defined, it requires 2 parameters passed into it (object taxresult, EventArgs e).

So if you want to call it you need to do something like

C#:
calculate_Single(null, null);

However after looking at your code, I don't think it is necessary to pass anything to those methods to make it work. Try this method:


C#:
[B]private void calculate_Single()[/B]
        {
            [B] int taxresult;[/B]
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);
            
            //Calculate using Single Filing Status tax brackets


            if (salaryint < 0){MessageBox.Show("What is wrong with you?  Put in a number above 0.");}
            else if (salaryint <= singleA) {taxresult = salaryint * bracketArate;}
            else if (salaryint <= singleB) {taxresult = salaryint * bracketBrate;}
            else if (salaryint <= singleC) {taxresult = salaryint * bracketCrate;}
            else if (salaryint <= singleD) {taxresult = salaryint * bracketDrate;}
            else if (salaryint <= singleE) {taxresult = salaryint * bracketErate;}
            else if (salaryint > singleE) {taxresult = salaryint * bracketFrate;}
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
            
            taxResultLabel.Text = taxresult.ToString();
        }


And to call it simply use

C#:
calculate_Single()
 
Thanks. When I made those changes, I get these two error messages:
No overload for 'calculate_Joint' matches delegate 'System.EventHandler'
No overload for 'calculate_Head' matches delegate 'System.EventHandler'

Note that it doesn't give that error message for calculate_Single for some reason. I've tried to find the difference between those methods, but they are pretty much identical. After making that change, I did make a few changes to troubleshoot and I think I put them back... but just in case, here is a fresh copy of my code:

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaxCalculator
{
    public partial class Form1 : Form
    {

        string salarynocomma;
        string salarycomma; 
        int salaryint;


        int singleA = 8700;
        int singleB = 35350;
        int singleC = 85650;
        int singleD = 178650;
        int singleE = 388350;
        int jointA = 17400;
        int jointB = 70700;
        int jointC = 142700;
        int jointD = 217450;
        int jointE = 388350;
        int headA = 12400;
        int headB = 47350;
        int headC = 122300;
        int headD = 198050;
        int headE = 388350;

        double bracketArate = .10;
        double bracketBrate = .15;
        double bracketCrate = .25;
        double bracketDrate = .28;
        double bracketErate = .33;
        double bracketFrate = .35;
//        double taxresultreturned;
        double taxresult;
        public Form1()
        {
            InitializeComponent();
        }

        
        private void testSalaryInput_Click(object sender, EventArgs e)
        {

        }

        private void calculate_Master(object sender, EventArgs e)
        {
          
            MessageBox.Show("You entered " + salarycomma + ".");

            if (filingStatusDropdown.Text == "Single")
            {
               calculate_Single();
                taxResultLabel.Text = taxresult.ToString();

            }
            else if(filingStatusDropdown.Text == "Married Filing Jointly")
            {
               calculate_Joint(); 
                taxResultLabel.Text = taxresult.ToString();
            }
            else if (filingStatusDropdown.Text == "Head of Household")
            {
               calculate_Head(); 
                taxResultLabel.Text = taxresult.ToString();
            }
            else 
            {
                MessageBox.Show("Please select a valid filing status.");
            }
            taxResultLabel.Text = taxresult.ToString();
            
        }

        private void calculate_Single()
        {
            double taxresult;
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);
            
            //Calculate using Single Filing Status tax brackets

            if (salaryint < 0){MessageBox.Show("What is wrong with you?  Put in a number above 0.");}
            else if (salaryint <= singleA) {taxresult = salaryint * bracketArate;}
            else if (salaryint <= singleB) {taxresult = salaryint * bracketBrate;}
            else if (salaryint <= singleC) {taxresult = salaryint * bracketCrate;}
            else if (salaryint <= singleD) {taxresult = salaryint * bracketDrate;}
            else if (salaryint <= singleE) {taxresult = salaryint * bracketErate;}
            else if (salaryint > singleE) {taxresult = salaryint * bracketFrate;}
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }

        private void calculate_Joint()
        {
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Joint Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= jointA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= jointB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= jointC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= jointD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= jointE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > jointE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }

        private void calculate_Head()
        {
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Head of Household Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= headA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= headB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= headC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= headD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= headE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > headE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
 
Hi Jcs,

not sure if you've managed to resolve this one yet, but try this below. If you still having any problems you can email me a copy of the project files and i can have a proper look for you - david.mcclay11@gmail.com

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaxCalculator
{
    public partial class Form1 : Form
    {

        string salarynocomma;
        string salarycomma; 
        int salaryint;


        int singleA = 8700;
        int singleB = 35350;
        int singleC = 85650;
        int singleD = 178650;
        int singleE = 388350;
        int jointA = 17400;
        int jointB = 70700;
        int jointC = 142700;
        int jointD = 217450;
        int jointE = 388350;
        int headA = 12400;
        int headB = 47350;
        int headC = 122300;
        int headD = 198050;
        int headE = 388350;

        double bracketArate = .10;
        double bracketBrate = .15;
        double bracketCrate = .25;
        double bracketDrate = .28;
        double bracketErate = .33;
        double bracketFrate = .35;
//        double taxresultreturned;
        double taxresult;
        public Form1()
        {
            InitializeComponent();
        }

        
        private void testSalaryInput_Click(object sender, EventArgs e)
        {

        }

        private void calculate_Master(object sender, EventArgs e)
        {
          
            MessageBox.Show("You entered " + salarycomma + ".");

            if (filingStatusDropdown.Text == "Single")
            {
               calculate_Single();
                taxResultLabel.Text = taxresult.ToString();

            }
            else if(filingStatusDropdown.Text == "Married Filing Jointly")
            {
               calculate_Joint(); 
                taxResultLabel.Text = taxresult.ToString();
            }
            else if (filingStatusDropdown.Text == "Head of Household")
            {
               calculate_Head(); 
                taxResultLabel.Text = taxresult.ToString();
            }
            else 
            {
                MessageBox.Show("Please select a valid filing status.");
            }
            taxResultLabel.Text = taxresult.ToString();
            
        }

        private void calculate_Single()
        {
            double taxresult;
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);
            
            //Calculate using Single Filing Status tax brackets

            if (salaryint < 0){MessageBox.Show("What is wrong with you?  Put in a number above 0.");}
            else if (salaryint <= singleA) {taxresult = salaryint * bracketArate;}
            else if (salaryint <= singleB) {taxresult = salaryint * bracketBrate;}
            else if (salaryint <= singleC) {taxresult = salaryint * bracketCrate;}
            else if (salaryint <= singleD) {taxresult = salaryint * bracketDrate;}
            else if (salaryint <= singleE) {taxresult = salaryint * bracketErate;}
            else if (salaryint > singleE) {taxresult = salaryint * bracketFrate;}
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }

        private void calculate_Joint()
        {
            double taxresult;
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Joint Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= jointA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= jointB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= jointC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= jointD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= jointE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > jointE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }

        private void calculate_Head()
        {
            double taxresult;           
            //Remove comma from salary input and convert it to int
            salarycomma = salaryTextBox.Text;
            salarynocomma = salarycomma.Replace(",", "");
            salaryint = int.Parse(salarynocomma);

            //Calculate using Head of Household Filing Status tax brackets

            if (salaryint < 0) { MessageBox.Show("What is wrong with you?  Put in a number above 0."); }
            else if (salaryint <= headA) { taxresult = salaryint * bracketArate; }
            else if (salaryint <= headB) { taxresult = salaryint * bracketBrate; }
            else if (salaryint <= headC) { taxresult = salaryint * bracketCrate; }
            else if (salaryint <= headD) { taxresult = salaryint * bracketDrate; }
            else if (salaryint <= headE) { taxresult = salaryint * bracketErate; }
            else if (salaryint > headE) { taxresult = salaryint * bracketFrate; }
            else
            {
                MessageBox.Show("I think something is wrong with what you put in.");
            }
        }
        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }

    }
}
 
After taking the last advice given, it still wouldn't work. However, it looks like once I took your advice, the problem wasn't the coding. It works like a charm now. The only thing I can think of that might have been different since the last time I tried it, was that I uninstalled a bunch of stuff and cleaned the registry. Thanks to you all for all your help!
 
Back
Top Bottom