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!
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: