Home > Enterprise >  Try/catch Unassigned local variable error in MS VisualStudio 2017
Try/catch Unassigned local variable error in MS VisualStudio 2017

Time:10-02

I've been having an issue laying out my code for a college assignment in which we have to create a cinema tickets booking application.

There are three separate categories of tickets available (Adult, student, children). We have to design a program that takes user input values for all three and calculates the total cost, total number of tickets sold, etc.

I'm trying to create individual try/catch blocks for each TextBox (Adult, Student, Child). However when I do, I get an unassigned local variable error message in the method. I'm not really sure why as it looks to me like it should be wrapped in properly. code is attached for reference.

If anyone can point me in the right direction, I'd be eternally grateful. I'm quite new to Visual Studio, and our university online resources are down at the moment due to a cyberattack.

Thanks, N

private void CalculateButton_Click(object sender, EventArgs e)

        {
        {
            try

            { int TotalAdults, TotalStudents, TotalChildren, TotalTicketsSold;
                decimal TotalReceipts;
            

                {try
                    {TotalAdults = int.Parse(AdultTextBox.Text); }
                    catch
                    {
                        MessageBox.Show("Please enter numerical data for number of adults",
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    }
                
                    try
                    { TotalStudents = int.Parse(StudentTextBox.Text); }
                    catch
                    {
                        MessageBox.Show("Please enter numerical data for number of students",
                          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                
                 try
                    {
                        TotalChildren = int.Parse(ChildTextBox.Text);
                    }
                    catch
                    {
                        MessageBox.Show("Please enter numerical data for number of children",
                          "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    
                TotalTicketsSold = TotalAdults   TotalStudents   TotalChildren;
                TotalReceipts = (TotalAdults * ADULT_TICKET_PRICE)  
                    (TotalStudents * STUDENT_TICKET_PRICE)   (TotalChildren * CHILD_TICKET_PRICE);
                CashierNameOutputLabel.Text = CashierNameTextBox.Text;
                CashierTotalTicketsSoldOutputLabel.Text = (TotalAdults   TotalStudents   TotalChildren).ToString("n");
                CashierTotalReceiptsOutputLabel.Text = TotalReceipts.ToString("c");

            }
            catch
            {
                MessageBox.Show("Please enter a decimal value in the spaces provided",
                   "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

CodePudding user response:

Just for a demo:

//Define the fare

public static int ADULT_TICKET_PRICE = 25;
public static int CHILD_TICKET_PRICE = 5;
public static int STUDENT_TICKET_PRICE = 10;

//Initialize the data to 0 to prevent call errors. Add a ‘Flag’: stop the operation when an error is reported.

int TotalAdults = 0, TotalStudents = 0, TotalChildren = 0, TotalTicketsSold = 0;
decimal TotalReceipts = 0;
bool Flag = false;//Flag to determine whether the input is normal.

//After modification:

private void CalculateButton_Click(object sender, EventArgs e) {
    int TotalAdults = 0, TotalStudents = 0, TotalChildren = 0, TotalTicketsSold = 0;
    decimal TotalReceipts = 0;
    bool Flag = false;//Flag to determine whether the input is normal.

    try { TotalAdults = int.Parse(AdultTextBox.Text); } catch {
        MessageBox.Show("Please enter numerical data for number of adults",
          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Flag = true;
        ;
    }

    try { TotalStudents = int.Parse(StudentTextBox.Text); } catch {
        MessageBox.Show("Please enter numerical data for number of students",
          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Flag = true;
    }

    try {
        TotalChildren = int.Parse(ChildTextBox.Text);
    } catch {
        MessageBox.Show("Please enter numerical data for number of children",
          "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Flag = true;
    }
    if (CashierNameTextBox.Text == "") {
        MessageBox.Show("Please enter a  value in the spaces provided",
           "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Flag = true;
    }
    if (!Flag) {
        TotalTicketsSold = TotalAdults   TotalStudents   TotalChildren;
        TotalReceipts = (TotalAdults * ADULT_TICKET_PRICE)  
            (TotalStudents * STUDENT_TICKET_PRICE)   (TotalChildren * CHILD_TICKET_PRICE);
        CashierNameOutputLabel.Text = CashierNameTextBox.Text;
        CashierTotalTicketsSoldOutputLabel.Text = TotalTicketsSold.ToString();//Ticket is a Int number
        CashierTotalReceiptsOutputLabel.Text = TotalReceipts.ToString("c");
    }
}

Output:

enter image description here

If you have any questions about my code, please add a comment below.

CodePudding user response:

Here is how I would do it:

private void CalculateButton_Click(object sender, EventArgs e)
{
  int TotalAdults = 0;
  int TotalStudents = 0;
  int TotalChildren = 0;
  int TotalTicketsSold = 0;
  decimal TotalReceipts = 0;
  const decimal ADULT_TICKET_PRICE = 20;
  const decimal STUDENT_TICKET_PRICE = 15;
  const decimal CHILD_TICKET_PRICE = 10;

  try
  {
    int.TryParse(AdultTextBox.Text, out TotalAdults);
  }
  catch (Exception)
  {
    MessageBox.Show("Please enter numerical data for number of adults",
          "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }

  try
  {
    int.TryParse(StudentTextBox.Text, out TotalStudents);
  }
  catch (Exception)
  {
    MessageBox.Show("Please enter numerical data for number of students",
        "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }

  try
  {
    int.TryParse(ChildTextBox.Text, out TotalChildren);
  }
  catch (Exception)
  {
    MessageBox.Show("Please enter numerical data for number of children",
        "Input error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }

  try
  {
    TotalTicketsSold = TotalAdults   TotalStudents   TotalChildren;
    TotalReceipts = (TotalAdults * ADULT_TICKET_PRICE)   (TotalStudents * STUDENT_TICKET_PRICE)   (TotalChildren * CHILD_TICKET_PRICE);
    CashierNameOutputLabel.Text = "Cashier's name: "   CashierNameTextBox.Text;
    CashierTotalTicketsSoldOutputLabel.Text = "Cashier Total Tickets Sold: "   (TotalAdults   TotalStudents   TotalChildren).ToString("n");
    CashierTotalReceiptsOutputLabel.Text = "Cashier Total Receipts: "   TotalReceipts.ToString("c");

  }
  catch
  {
    MessageBox.Show("Please enter a decimal value in the spaces provided",
       "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}

You can find the whole solution on screen shot

  • Related