【问题标题】:Repetative "If statements" save and reuse results重复的“If 语句”保存和重用结果
【发布时间】:2018-04-11 13:24:58
【问题描述】:

我在 MVC(剃刀代码)的 Visual Studio 视图中。我在代码的不同区域多次使用相同的布尔“if statemant”。有人告诉我,我可以保存第一个 if 语句的结果并重复使用结果,这样我就不必多次编写“if 语句”了。

如何保存第一个 if 语句的结果并重复使用下面的结果?

var IsManager = someRole;
var IsTeamLead= someOtherRole;

if(IsManager || ITeamLead)
{
 //Veiew this resticted content on the application;
}

//Code;
//Code;

if(IsManager || IsTeamLead)
{
 //Veiew more resticted content on the application;
}

//Little more code;
//Little more Code;

if(IsManager || Is TeamLead)
{
 //Veiew this content too;
}

//Little more code
//Little more code
//Little more code

if(IsManager || IsTeamLead)
{
 //Veiew this content;
}

【问题讨论】:

  • 我不确定他们给你的建议,但一种可能是this one
  • 引入一个新变量,您可以像bool isAuthorizedToView = someRole || someOtherRole; 一样检查它现在您可以在任何地方使用if(isAuthorizedToView){...}

标签: c# if-statement razor save code-reuse


【解决方案1】:
bool isManagerOrTeamLead = IsManager || ITeamLead;

那么……

if(isManagerOrTeamLead)
{
 //Veiew this resticted content on the application;
}

【讨论】:

  • rory.ap 感谢您的确认!我对那个的想法。
猜你喜欢
  • 2018-12-05
  • 1970-01-01
  • 2015-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 1970-01-01
相关资源
最近更新 更多