Please help with commission switch statement + calc

C Dull

Member
Joined
Jun 15, 2021
Messages
10
Programming Experience
Beginner
Hey,

I'm new to C# and coding in general. I've done some reading and online "hands-on" tutorials but I'm truly stuck here. Can someone please help me out and review the code I have written? I'm willing to put the time in to learn, I just don't know where to go from here.

I'm having a problem with my switch statement that involves different levels of commission rates, as well as getting the calculation to work. Also, on my last line it is giving me an error -- saying that "dblCalculateCommission" is an unassigned variable. I haven't had this problem on similar exercises. Appreciate any help, ready to pull my hair out!
C#:
    private void btnCalculateCommission_Click(object sender, EventArgs e)
        {
            // declare variables to be used in the calculation
            double dblSalesPersonLevel;
            double dblAmountSold;
            double dblCalculateCommission;

            // convert the values in the text boxes to numeric and place into variables
            dblSalesPersonLevel = Convert.ToDouble(txtSalesPersonLevel.Text);
            dblAmountSold = Convert.ToDouble(txtAmountSold.Text);

            // check the sales level is between 1 and 4, if it is outside of these parameters display an error message
            if (dblSalesPersonLevel < 1)
            {
                MessageBox.Show("Sales level cannot be less than 1", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }
            else if (dblSalesPersonLevel > 4)
            {
                MessageBox.Show("Sales level cannot be greater that 4", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }


            // Calculate commissions based on sales person level

            {
                string strSalesPersonLevel;
                strSalesPersonLevel = txtSalesPersonLevel.Text;

                switch (strSalesPersonLevel)
                {
                    case "1":
                        {
                            dblCalculateCommission = 500 + (dblAmountSold * 0.02);
                            break;
                        }
                    case "2":
                        {
                            dblCalculateCommission = 750 + (dblAmountSold * 0.03);
                            break;
                        }
                    case "3":
                        {
                            dblCalculateCommission = 1000 + (dblAmountSold * 0.04);
                            break;
                        }
                    case "4":
                        {
                            dblCalculateCommission = 1250 + (dblAmountSold * 0.05);
                            break;
                        }

                }
                // Convert commission into a string and place into a label with currency
                //formatting
                lblCalculateCommission.Text = dblCalculateCommission.ToString("C2");
 
Last edited by a moderator:
Also, on my last line it is giving me an error -- saying that "dblCalculateCommission" is an unassigned variable.
This is because on line 6 you just declared the variable, but didn't initialize it any value. The C# compilers believes that there are some code paths between line 6 and 59 where you will never assign a value to that variable. Since this will lead to an error condition were an uninitialized variable will be used, it's letting you know. The simplest solution is to declare and initialize the variable up in line 6. It's a good programming habit to get into.
 
I'm having a problem with my switch statement that involves different levels of commission rates, as well as getting the calculation to work.
What problems are you running into? What error messages are you getting? Or you getting unexpected behavior? What behavior were you expecting to see?
 
What problems are you running into? What error messages are you getting? Or you getting unexpected behavior? What behavior were you expecting to see?

Hey Skydiver,

I appreciate you taking the time and bearing with a new guy. I find this very interesting but it's certainly a challenge.

The objective of this exercise is to calculate commission at 4 different salesperson levels:
Level 1: Gets $500 + 2% of sales
Level 2: Gets $750 + 3% of sales
Level 3: Gets $1000 + 4% of sales
Level 4: Gets $1250 + 5% of sales

The user inputs their sales level and amount sold into text boxes -- I'm attempting to have the calculation display on a label after hitting a "calculate commission" label.

I made the correction as per your recommendation to initialize a value to dblCalculateCommission.

I'm now getting error CS1513 -- stating that the compiler is expecting a closing curly brace. I checked over it and all my braces are indeed closed, as indicated by dotted lines connecting brace pairs. This error is only allowing me to run from my last successful build. If you wouldn't mind having another look. One day I'll pay it forward to another scrub starting out.
C#:
  private void btnCalculateCommission_Click(object sender, EventArgs e)
        {
            // declare variables to be used in the calculation
            double dblSalesPersonLevel;
            double dblAmountSold;
            double dblCalculateCommission;

            // convert the values in the text boxes to numeric and place into variables
            dblSalesPersonLevel = Convert.ToDouble(txtSalesPersonLevel.Text);
            dblAmountSold = Convert.ToDouble(txtAmountSold.Text);
            dblCalculateCommission = Convert.ToDouble(lblCalculateCommission.Text);


            // check the sales level is between 1 and 4, if it is outside of these parameters display an error message
            if (dblSalesPersonLevel < 1)
            {
                MessageBox.Show("Sales level cannot be less than 1", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }
            else if (dblSalesPersonLevel > 4)
            {
                MessageBox.Show("Sales level cannot be greater that 4", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }


            // Calculate commissions based on sales person level


            string strSalesPersonLevel;
            strSalesPersonLevel = txtSalesPersonLevel.Text;

            switch (strSalesPersonLevel)
            {
                case "1":
                    {
                        dblCalculateCommission = 500 + (dblAmountSold * 0.02);
                        break;
                    }
                case "2":
                    {
                        dblCalculateCommission = 750 + (dblAmountSold * 0.03);
                        break;
                    }
                case "3":
                    {
                        dblCalculateCommission = 1000 + (dblAmountSold * 0.04);
                        break;
                    }
                case "4":
                    {
                        dblCalculateCommission = 1250 + (dblAmountSold * 0.05);
                        break;
                    }


            }


            // Convert commission into a string and place into a label with currency
            //formatting

            lblCalculateCommission.Text = dblCalculateCommission.ToString("C2");
        }
 
Last edited by a moderator:
Please remember to put your code in code tags. It's the icon on the toolbar that looks like "</>". Going to fix your post again for you.

Post the complete error message. It'll give you the line number and character position where it is expecting to see a closing curly brace. Is it in the code that you showed us in post #4, or somewhere else?

As an aside, the vertical lines are not the true indicators of matching braces, they only give you a hint of where the closing brace should be. Unless you've overridden the key mappings in VS2019, Ctrl-] should take you to the matching brace from the point of view of the compiler.
 
Last edited:
If you really do have matching braces and the compiler is telling you otherwise then your system is broken and there's nothing we can do about that. More likely, though, is that you just think that you have matching braces and you don't. Maybe they match in the code snippet you copied here but not for the entire file. Copy all the code in the file, paste it here, format it as code as instructed and tell us exactly where the compilation error directs you, so we know where to look.
 
If you really do have matching braces and the compiler is telling you otherwise then your system is broken and there's nothing we can do about that. More likely, though, is that you just think that you have matching braces and you don't. Maybe they match in the code snippet you copied here but not for the entire file. Copy all the code in the file, paste it here, format it as code as instructed and tell us exactly where the compilation error directs you, so we know where to look.

Sorry about the format of my last post and I appreciate the help. I'm willing to learn and put the work in.

The compilation error is on the final line of the code, with the closed curly bracket.

Also, when I try to debug I get the message:
"
Build started...
1>------ Build started: Project: Salesperson Commission Calculator, Configuration: Debug Any CPU ------
1>C:\Users\koval\source\repos\Salesperson Commission Calculator\Salesperson Commission Calculator\frmSalespersonCommissionCalculator.cs(117,10,117,10): error CS1513: } expected
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped =========="

Sales Commission Calculator:
// Purpose  :   This program calculates sales commissions based on tiered salesperson levels
//
//****************

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Salesperson_Commission_Calculator
{
    public partial class frmSalespersonCommissionCalculator : Form
    {
        public frmSalespersonCommissionCalculator()
        {
            InitializeComponent();
        }

        private void frmSalespersonCommissionCalculator_Load(object sender, EventArgs e)
        {

        }

        private void btnCalculateCommission_Click(object sender, EventArgs e)
        {
            // declare variables to be used in the calculation
            double dblSalesPersonLevel;
            double dblAmountSold;
            double dblCalculateCommission;

            // convert the values in the text boxes to numeric and place into variables
            dblSalesPersonLevel = Convert.ToDouble(txtSalesPersonLevel.Text);
            dblAmountSold = Convert.ToDouble(txtAmountSold.Text);
            dblCalculateCommission = Convert.ToDouble(lblCalculateCommission.Text);


            // check the sales level is between 1 and 4, if it is outside of these parameters display an error message
            if (dblSalesPersonLevel < 1)
            {
                MessageBox.Show("Sales level cannot be less than 1", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }
            else if (dblSalesPersonLevel > 4)
            {
                MessageBox.Show("Sales level cannot be greater that 4", "Input Error");
                txtSalesPersonLevel.SelectAll();
                txtSalesPersonLevel.Select();
            }

            // check the sales amount is between $1 and $100,000
            if (dblAmountSold < 1)
            {
                MessageBox.Show("Amount sold cannot be less than $1", "Input Error");
                txtAmountSold.SelectAll();
                txtAmountSold.Select();
            }
            else if (dblAmountSold > 100000)
            {
                MessageBox.Show("Amount sold cannot be more than $100,000", "Input Error");
                txtAmountSold.SelectAll();
                txtAmountSold.Select();
            }

            // Calculate commissions based on sales person level


            string strSalesPersonLevel;
            strSalesPersonLevel = txtSalesPersonLevel.Text;

            switch (strSalesPersonLevel)
            {
                case "1":
                    {
                        dblCalculateCommission = 500 + (dblAmountSold * 0.02);
                        break;
                    }
                case "2":
                    {
                        dblCalculateCommission = 750 + (dblAmountSold * 0.03);
                        break;
                    }
                case "3":
                    {
                        dblCalculateCommission = 1000 + (dblAmountSold * 0.04);
                        break;
                    }
                case "4":
                    {
                        dblCalculateCommission = 1250 + (dblAmountSold * 0.05);
                        break;
                    }


            }


            // Convert commission into a string and place into a label with currency
            //formatting

            lblCalculateCommission.Text = dblCalculateCommission.ToString("C2");
        }

Thank you

Brody,
 
Based on the code you just posted, you're missing two closing braces. The last brace in that code closes the btnCalculateCommission_Click method but then there's no closing brace for either the class or the namespace.
 
Based on the code you just posted, you're missing two closing braces. The last brace in that code closes the btnCalculateCommission_Click method but then there's no closing brace for either the class or the namespace.

Thank you to yourself and Skydiver!

Going to do your beginner tutorial as well.
 
Back
Top Bottom