Friday, April 11, 2014

Optional Parameter

Notes (04/11/2014): Optional Parameter
To read more: Click here




namespace Zau_Optional_Parameters
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double CalculateBMI(double weight=150, double height = 70)
        {
            return (weight * 703) / (height * height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //User Input
                double h = double.Parse(txtHeight.Text);
                double w = double.Parse(txtWeight.Text);
                //call the CalculateBMI method by position
                double bmi = CalculateBMI(w, h);
                //the value of h (first position) is passed to 
                //the parameter  height (frist position)
                //the value of w (second position) is passed to 
                //the parameter weight (second position)
                txtBMI.Text = bmi.ToString("f1");
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message, "Format Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                //User Input
                double h = double.Parse(txtHeight.Text);
                double w = double.Parse(txtWeight.Text);
                //call the CalculateBMI method using name arguments.
                double bmi = CalculateBMI(height:h, weight:w);
                txtBMI.Text = bmi.ToString("f1");
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message, "Format Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //use optional arguments
            //the CalculateBMI method define a default value for the height.
            //If you don't pass the height value to the method,
            //it will use default value.
            try
            {
                //User Input
                double w = double.Parse(txtWeight.Text);
                //call the CalculateBMI method using name arguments.
                double bmi = CalculateBMI(w);
                //height is optional, a value of 70 is used as defined by method.
                txtBMI.Text = bmi.ToString("f1");
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message, "Format Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            double bmi = CalculateBMI();
            //both height and weight are optionals.
            //the CalculateBMI method will use 150 for weight and 70 for height;
            txtBMI.Text = bmi.ToString("f1");
        }
    }
}


//==================Home Work===========================
Read For Next Week.
* Enumeration
* ComboBox

No comments:

Post a Comment