Skip to main content
 首页 » 编程设计

c#之可以切换出在 if/else 方法中返回的值吗

2024年02月20日12myhome

我在 Calculations.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,其中最终返回是 pricePaymentPlanChapter13。我认为可能有一种方法可以将其关闭。

否则,我必须执行以下操作:

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); 
        ... 
    } 

这样的事情可行吗(以及如何可行)?谢谢。感谢任何代码示例或指导。

更新: 这是我的 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); 
} 

此外,我在 PriceQuote 中为第 7 章和第 13 章设置了一个值(例如,public static decimal priceChapter7 { get { return 799; } }

请您参考如下方法:

如果不更多地了解您在做什么,很难确定建议,但如果您的方法之间的唯一区别是一组要使用的值(一组用于第 7 章,另一组用于第 13 章),则可能有意义从 PriceQuote 中取出这些值并创建一个基类型来保存这些值。那么您的 decPaymentPlan 和其他方法只会采用该类型的实例。例如:

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

然后,更改您的方法以获取 Chapter 参数

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; 
} 

现在您只需要 Chapter 的两个实例,一个用于 7,另一个用于 13,并相应地调用您的计算方法。

更新:为了详细说明我所说的“相应地调用您的计算方法”的意思,假设您有两个静态变量(在您的应用程序中有意义的地方,可能在计算中.cs)

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

然后在你的 Controller 中,你就可以写

ViewBag.Chapter7Total = calc.CalculatePrice(quoteData, Chapter7); 
ViewBag.Chapter13Total = calc.CalculatePrice(quoteData, Chapter13);