Skip to content

If statement

Ryan Hammond

Asked Oct 1, 2011, 9:55 AM EDT1 answer
Share
LinkedInX
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;
        }
    }
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    solved. there was some weird conflict with the variable.

    public MainWindow()
            {
                InitializeComponent();
            }

            // Initalize ref & out variables
            int getNumOfTix;
            double getPricePer;
            double getSubTotal;
            double getTax;
            double getGrandTotal;

            private void btnCalc_Click(object sender, RoutedEventArgs e)
            {
                // if the user did not enter a ticket amount greater than 0
                if (txtNumOfTickets == 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(out getNumOfTix, out getPricePer, ref getSubTotal, ref getTax, ref getGrandTotal);
            }

            private void calcAmount(out int getNumOfTix, out double getPricePer, ref double getSubTotal, ref double getTax, ref double getGrandTotal)
            {
                const double HST_SALES_TAX = 0.13;

                int numOfTix = int.Parse(txtNumOfTickets.Text);
                getNumOfTix = numOfTix;

                double pricePer = double.Parse(txtPricePerTicket.Text);
                getPricePer = pricePer;

                // 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;
            }
        }
    }
    0