Ryan Hammond

Ryan Hammond

  • NA
  • 12
  • 5.9k

If statement

Oct 1 2011 1:55 PM
Hey guys,

The following code works perfectly, except the If statement. When the value of the textbox is null, it just crashes the program instead of handling the error. Any help is appreciated.


        public MainWindow()
        {
            InitializeComponent();
        }

        // Initalize ref variables
        double getSubTotal;
        double getTax;
        double getGrandTotal;

        private void btnCalc_Click(object sender, RoutedEventArgs e)
        {
            // Assign variable and collect user input
            int numOfTix = int.Parse(txtNumOfTickets.Text);
            double pricePer = double.Parse(txtPricePerTicket.Text);

           // if the user did not enter a ticket amount
            if (txtNumOfTickets.Text == null || txtNumOfTickets.Text == "")
            {
                txtNumOfTickets.Focus();
                MessageBox.Show("Please enter the number of tickets you would like to purchase.");
            }
            else
                // Call method to calculate amounts
                calcAmount(numOfTix, pricePer, ref getSubTotal, ref getTax, ref getGrandTotal);
        }

        private void calcAmount(int numOfTix, double pricePer, ref double getSubTotal, ref double getTax, ref double getGrandTotal)
        {
            const double HST_SALES_TAX = 0.13;

            // Calculate prices
            double subTotal = numOfTix * pricePer;
            double tax = subTotal * HST_SALES_TAX;
            double total = tax + subTotal;

            // Assign values to ref variables
            getSubTotal = subTotal;
            getTax = tax;
            getGrandTotal = total;

            // Assign values to textbox
            txtSubTotal.Text = subTotal.ToString("C");
            txtHSTTax.Text = tax.ToString("C");
            txtTotal.Text = total.ToString("C");
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void radioOrchestra_Checked(object sender, RoutedEventArgs e)
        {
            string orchestraSeating = "40.00";
            txtPricePerTicket.Text = orchestraSeating;
        }

        private void radioMezzanine_Checked(object sender, RoutedEventArgs e)
        {
            string mezzanineSeating = "27.50";
            txtPricePerTicket.Text = mezzanineSeating;
        }

        private void radioBalcony_Checked(object sender, RoutedEventArgs e)
        {
            string balconySeating = "15.00";
            txtPricePerTicket.Text = balconySeating;
        }

        private void radioGeneral_Checked(object sender, RoutedEventArgs e)
        {
            string generalSeating = "10.00";
            txtPricePerTicket.Text = generalSeating;
        }
    }
}


Answers (1)