Is it possible to rewrite this code and use the oper string oin the actual operation

T

Tony Johansson

Hello!

Below is some code that I hope can be rewritten and make use of the oper
string in the actual calculation
If I could rewite something like this
txtResult.Text = string.Format("{0:F3}", Convert.ToDouble(txtNumber1.Text)
oper Convert.ToDouble(txtNumber2.Text));
I would get very short code. But how can I do that ?

protected void Operation_Click(object sender, EventArgs e)
{
string oper = ((Button)sender).Text;

switch (oper)
{
case "+" : txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) + Convert.ToDouble(txtNumber2.Text));
break;
case "-": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) - Convert.ToDouble(txtNumber2.Text));
break;
case "*": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) * Convert.ToDouble(txtNumber2.Text));
break;
case "/": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) / Convert.ToDouble(txtNumber2.Text));
break;
}
}

//Tony
 
U

Ulrik Magnusson

Not really answering your question, but I would probably do it
like this - not very short, but very readable (I think):

protected void Operation_Click(object sender, EventArgs e)
{
string oper = ((Button)sender).Text;

// input validation and conversion
// todo: use Double.TryParse()
double d1 = Convert.ToDouble(txtNumber1.Text);
double d2 = Convert.ToDouble(txtNumber2.Text);

// calculation
double result = Calculate(oper, d1, d2);

// output
txtResult.Text = string.Format("{0:F3}", result);
}

private static double Calculate(string oper, double d1, double
d2)
{
switch (oper)
{
case "+": return d1 + d2;
case "-": return d1 - d2;
case "*": return d1 * d2;
case "/": return d1 / d2;
default: throw new InvalidOperationException(oper);
}
}

You could also go with the Strategy pattern, but it seems
like way overkill for this one. http://en.wikipedia.org/wiki/Strategy_pattern.

I don't think there's any easy way to execute a string as code in C# -
your options are probably
1: use CSharpCodeProvider to build and execute an assembly at runtime
2: a third party script engine - for example CS Script http://www.csscript.net/
(I haven't tried it)
 
A

Arne Vajhøj

Below is some code that I hope can be rewritten and make use of the oper
string in the actual calculation
If I could rewite something like this
txtResult.Text = string.Format("{0:F3}", Convert.ToDouble(txtNumber1.Text)
oper Convert.ToDouble(txtNumber2.Text));
I would get very short code. But how can I do that ?

protected void Operation_Click(object sender, EventArgs e)
{
string oper = ((Button)sender).Text;

switch (oper)
{
case "+" : txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) + Convert.ToDouble(txtNumber2.Text));
break;
case "-": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) - Convert.ToDouble(txtNumber2.Text));
break;
case "*": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) * Convert.ToDouble(txtNumber2.Text));
break;
case "/": txtResult.Text = string.Format("{0:F3}",
Convert.ToDouble(txtNumber1.Text) / Convert.ToDouble(txtNumber2.Text));
break;
}
}

You can not do that in traditional C# syntax.

There are certain possibilities with tricks like embedded
script interpreters and dynamic code generation.

But I think that the switch is OK. It is easy to
read what the code does.

As a matter of principle I would use double.Parse instead
of Convert.ToDouble, but that is not related to your question.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top