Basic c# coding

 This class can be use to perform basic mathematical operations


 /* thi class have a function on performing some basic operations in c# addition subtraction
        multplication and division having only one function
     */


    class cls_MDAS
    {
        /// <summary>
        /// simple mdas have a parameter of first_num, last_num and opt this firstnum and second num must be a number
        /// </summary>
        /// <param name="firstNum"></param>
        /// <param name="lastNum"></param>
        /// <param name="opt"></param>
        /// <returns></returns>
        public string simple_mdas(int firstNum, int lastNum,string opt)
        {
            string result = "";// declaration of a variable `

            switch (opt) //  checks the value of opt
            {
                case "+":
                    // if opt  equals to  +  then this code will execute
                    result = (firstNum + lastNum) .ToString();
                    break;
                case "-":
                    // if opt  equals to  -  then this code will execute
                    result = (firstNum - lastNum) .ToString();
                    break;
                case "*":
                    // if opt  equals to  *  then this code will execute
                    result = (firstNum * lastNum) .ToString();
                    break;
                case "/":
                    // if opt  equals to  /  then this code will execute
                    //checks if first num =0

                    if (firstNum != 0)
                    {
                        if (firstNum >= lastNum)
                        {
                            result = (firstNum / lastNum).ToString();
                        }
                        else
                        {
                            result = "first number must be greater than the second number";

                        }
                    }
                    else
                    {
                        result = "first number must not be equal to 0";

                   
                    }


                        break;
                default:
                        result = "Please select from the choices";

            }
            //returns a result if a result base from the operations above or an error

            return result;

       
        }



    }


to call the classes from the function  you need to include this code on  a combobox event SelectedIndexChanged
try is used  when an expected error might be encounter  catch method will be executed if theirs an error


try
            {
                cls_MDAS cls = new cls_MDAS(); //  instantiation of  cls_MDAS for us to use the function that are declared public

                string main_result=cls.simple_mdas(int.Parse(txtFirstNumber.Text), int.Parse(txtSecondNumber.Text),cbochoices.Text);
   MessageBox.Show(main_result);

// string  main_result value wull hold the returned value of function simple_mdas  depending on the parameter u send

            }
            catch
            {
                MessageBox.Show("input some number");

            }

Walang komento:

Mag-post ng isang Komento