English 中文(简体)
能否切换在 if/else 方法中返回的值?
原标题:Possible to switch out a value that s being returned in an if/else method?
  • 时间:2012-05-24 17:22:23
  •  标签:
  • c#

我在 Calcudules.cs 类中有以下代码:

public decimal decPaymentPlan(QuoteData quoteData)
    {
        if (quoteData.StepFilingInformation.PaymentPlanRadioButton 
            == StepFilingInformation.PaymentPlan.No)
            return PriceQuote.priceNoPaymentPlan;
        else
            return PriceQuote.pricePaymentPlanChapter7; //may want to switch
                                                        //to Chapter13 value
    }

public decimal CalculateChapter7(QuoteData quoteData)
    {
        decimal total = PriceQuote.priceChapter7;
        total += this.decPaymentPlan(quoteData); //want to be able to tell
                                                 //which to use, 7 or 13
        return total;
    }

我想看看我能不能避免一个额外的 decPaymentPlan 最后的回报是 pricePaymentPlanChode13 。 我想也许有办法把它切换出来。

否则,我必须做以下工作:

public decimal decPaymentPlanChapter7(QuoteData quoteData)
    {
        ...
        else
            return PriceQuote.pricePaymentPlanChapter7;
    }

public decimal decPaymentPlanChapter13(QuoteData quoteData)
    {
        ...
        else
            return PriceQuote.pricePaymentPlanChapter13;
    }

...

//the following will appear anyway, but rather than just using
//one method call which switches the choice based on something
public decimal CalculateChpater7(QuoteData quoteData)
    {
        ...
        //instead of decPaymentPlan(quoteData) + something to switch
        total+= this.decPaymentPlanChapter7(quoteData);
        ...
    }

public decimal CalculateChpater13(QuoteData quoteData)
    {
        ...
        //instead of decPaymentPlan(quoteData) + something to switch
        total+= this.decPaymentPlanChapter13(quoteData);
        ...
    }

谢谢 感谢任何代码样本或指导

UPDATE: This is my controller:

public ActionResult EMailQuote()
{
    Calculations calc = new Calculations();

    QuoteData quoteData = new QuoteData
    {
        StepFilingInformation = new Models.StepFilingInformation
        {
            //just moking user input here temporarily to test out the UI
            PaymentPlanRadioButton = Models.StepFilingInformation.PaymentPlan.Yes,
        }
     };

     var total = calc.CalculatePrice(quoteData);
     ViewBag.CalculatePrice = total; // ADDED THIS LINE
     return View(quoteData);
}

另外,我为第7章和第13章设定了一个报价(例如,公共定位小数价格)第7章{得到{返回799;}/code>

最佳回答

很难确定建议是否正确,而不能更深入地了解你正在做什么,但是,如果你们方法之间的唯一区别是使用一组数值(第7章为1个,第13章为1个),那么将这些数值从PriceQuote中取出并创建一种基本类型以保持这些数值是有道理的。然后,您的“取消支付计划”和其他方法将只采用这种类型的实例。例如:

class Chapter // for lack of a better name
{
    public decimal PaymentPlan { get; set; }
    public decimal Price { get; set; }
    ....
}

然后,改变您使用章节参数的方法

public decimal decPaymentPlan(QuoteData quoteData, Chapter chapter)
{
    if (quoteData.StepFilingInformation.PaymentPlanRadioButton 
        == StepFilingInformation.PaymentPlan.No)
        return PriceQuote.priceNoPaymentPlan;
    else
        return chapter.PaymentPlan;
}

public decimal Calculate(QuoteData quoteData, Chapter chapter)
{
    decimal total = chapter.Price;
    total += this.decPaymentPlan(quoteData, chapter);

    return total;
}

现在,你只需要两个章节,一个7章,另一个13章, 并相应调用你的计算方法。

UPDATE :要细化一下我的意思,将计算方法称为相应的计算方法,举例说,你有两个静态变量(在应用中有道理的地方,或许在计算.cs中)。

static Chapter Chapter7 = new Chapter() { Price = 799.99, PaymentPlan = 555.55 };
static Chapter Chapter13 = ...

然后,在你的控制器里,你可以写作

ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7);
ViewBag.Chapter13Total = calc.CalculatePrice(quoteData, Chapter13);
问题回答

7和13有什么区别?

if (quoteData.StepFilingInformation.PaymentPlanRadioButton ==
StepFilingInformation.PaymentPlan.No)              
      return PriceQuote.priceNoPaymentPlan;          
else if (//whatever fulfills ch. 7)             
      return PriceQuote.pricePaymentPlanChapter7;
else //ch. 13
      return PriceQuote.pricePaymentPlanChapter13;

看起来您可以创建一个 chapts 的编号,并将该编号作为第二个参数传送到 decPaymentPlan 方法是?

你将你的商业逻辑 与你的直观层混为一谈:

if (quoteData.StepFilingInformation.PaymentPlanRadioButton == StepFilingInformation.PaymentPlan.No)

更好的设计是有一个模型,据以进行修改,例如MVC、MVP、MVVM。

示例:

public class View
{
    private Model _model = new Model();

    public View()
    {
    }

    public Controller Controller
    {
        get;
        set;
    }

    private void OnButton1Click(object sender, EventArgs args)
    {
        _model.Option = Options.Option1;
    }

    private void OnSaveClick(object sender, EventArgs args)
    {
        if (Controller != null)
            Controller.ApplyChanges(_model);
    }
}

然后,控制器可以应用商业逻辑,不受观察结构的影响,这样你就可以自由地改变其中任何一个。

例如:

public class Controller
{
    Model Model
    {
        get;
        set;
    }

    decimal CalculateSum()
    {
        return Model.Items.Aggregate((a, b) => a + b);
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...