简体   繁体   English

在 MS VisualStudio 2017 中尝试/捕获未分配的局部变量错误

[英]Try/catch Unassigned local variable error in MS VisualStudio 2017

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).我正在尝试为每个文本框(成人、学生、儿童)创建单独的 try/catch 块。 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.我是 Visual Studio 的新手,由于网络攻击,我们大学的在线资源目前已关闭。

Thanks, N谢谢,ñ

private void CalculateButton_Click(object sender, EventArgs e) 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);
            }
        }

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. //将数据初始化为0,防止调用错误。 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:输出:

在此处输入图像描述

If you have any questions about my code, please add a comment below.如果您对我的代码有任何疑问,请在下面添加评论。

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 GitHub您可以在GitHub上找到完整的解决方案

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM