1
 public int MainOperationSimplifeid(char operatoru)
    {
        if (beforeoperation == 2)
        {
            a2 = Convert.ToInt32(textBox1.Text);
            textBox1.Text = "";
            result = a1 operatoru a2;
            //   textBox1.Text = Convert.ToString(result);
            a1 = 0;
            a2 = 0;
        }
        beforeoperation++;
        return result;
    }

a1, a2 - represents two numbers in the program and result is the answer for the >operation peformed

i am thinking of using one single character or some other like argument that reduce all my operators used else where in the program

but i cant get the +, * as replace to a char between two integers. :(

Can you guys please help which inbult function or argument can replace all my operator to a single variable , so that i can pass that as my argument.

Thanks for going through my question :)

2 Answers 2

2

This sort of thing can be done with delegates. The built in delegate type Func<T1, T2, T3> represents code that accepts two parameters and returns a result.

public int MainOperationSimplifeid(Func<int, int, int> operatoru)
{
    if (beforeoperation == 2)
    {
        a2 = Convert.ToInt32(textBox1.Text);
        textBox1.Text = "";
        result = operatoru(a1, a2);
        //   textBox1.Text = Convert.ToString(result);
        a1 = 0;
        a2 = 0;
    }
    beforeoperation++;
    return result;
}

Then you can call the method with a lambda:

var addResult = MakeOperationSimplifeid((x, y) => x + y);
var multResult = MakeOperationSimplifeid((x, y) => x * y);
-1

I think you can't make that, because the compiler needs the operator to compile the program. So, as I can see, the solution could be a enum, Something Like this:

Enum Operator {
     Addition, Substraction, Multiplication, Division 
};

public double MainOperationSimplified(Operator operatoru)
{
    if (beforeoperation == 2)
    {
        a2 = Convert.ToInt32(textBox1.Text);
        textBox1.Text = "";

        switch (operatoru) {
            case Addition:
               result = a1 + a2;
               break;
            case Substraction:
               result = a1 - a2;
               break;
            case Multiplication:
               result = a1 * a2;
               break;
            case Division:
               result = a1 / a2;
               break;
            default:
               result = 0;
               break;
        }

        a1 = 0;
        a2 = 0;
    }
    beforeoperation++;
    return result;
}
1
  • Thanks for an answer, still i am expecting a little easy answer or to mean more direct answer. Switch case of 4 would look nice but i am thinking of a very bigger operator stack , supplying my own operators for a better functinality
    – Neoni Bush
    Jul 24, 2012 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.