What follows is a refactoring of the second controller here. Notice that I had to use Action and that I made Raiser much simplier. I read of Math.Pow today in: C# 4.0 in a Nutshell
using System;
namespace DelegateExample.Controllers
{
public class CalculationController : BaseController
{
private string thirdValue;
public RenderJsonResult Go(string act, string one, string two)
{
Int32 firstInteger = Convert.ToInt32(one);
Int32 secondInteger = Convert.ToInt32(two);
Action mathDelegate;
switch(act)
{
case "add":
mathDelegate = () => Adder(firstInteger, secondInteger);
break;
case "multiply":
mathDelegate = () => Multiplier(firstInteger, secondInteger);
break;
case "power":
mathDelegate = () => Raiser(firstInteger, secondInteger);
break;
default:
mathDelegate = () => StatusQuoSustainer(firstInteger);
break;
}
mathDelegate();
var result = new { calulation = thirdValue };
return RenderJson(result);
}
private void Adder(Int32 firstInteger, Int32 secondInteger)
{
thirdValue = (firstInteger + secondInteger).ToString();
}
private void Multiplier(Int32 firstInteger, Int32 secondInteger)
{
thirdValue = (firstInteger * secondInteger).ToString();
}
private void Raiser(Int32 firstInteger, Int32 secondInteger)
{
thirdValue = Math.Pow(firstInteger,secondInteger).ToString();
}
private void StatusQuoSustainer(Int32 loneInteger)
{
thirdValue = loneInteger.ToString();
}
}
}
No comments:
Post a Comment