gpt4 book ai didi

C#如何缩短多个If表达式

转载 作者:行者123 更新时间:2023-12-03 16:29:27 26 4
gpt4 key购买 nike

我有多个验证 bool 属性,可能需要或不需要。如果需要它们,则需要对其进行检查以进行验证。因此,我必须构建多个 if 语句来处理每个属性。我的问题是是否有更好的方法来维护它,而不是为每个新属性编写 if 。

public class ProductionNavbarViewModel
{
public bool IsProductionActive { get; set; }
public bool ValidatedComponents { get; set; }
public bool ValidatedGeometries { get; set; }
public bool ValidatedPokayokes { get; set; }
public bool ValidatedTechnicalFile { get; set; }
public bool ValidatedStandardOperationSheet { get; set; }
public bool ValidatedOperationMethod { get; set; }
public bool IsComponentsRequired { get; set; }
public bool IsGeometriesRequired { get; set; }
public bool IsPokayokesRequired { get; set; }
public bool IsTechnicalFileRequired { get; set; }
public bool IsStandardOperationSheetRequired { get; set; }
public bool IsOperationMethodRequired { get; set; }


public bool IsProductionReadyToStart()
{
if (IsComponentsRequired)
{
return ValidatedComponents;
}

if (IsComponentsRequired && IsGeometriesRequired)
{
return ValidatedComponents && ValidatedGeometries;
}

if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes;
}

if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile;
}

if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && ValidatedStandardOperationSheet)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet;
}

if (IsComponentsRequired && IsGeometriesRequired && IsPokayokesRequired && IsTechnicalFileRequired && IsStandardOperationSheetRequired && IsOperationMethodRequired)
{
return ValidatedComponents && ValidatedGeometries && ValidatedPokayokes && ValidatedTechnicalFile && ValidatedStandardOperationSheet && ValidatedOperationMethod;
}

return false;
}
}

编辑

编写此代码时出现问题。目的是验证所有选项,它们必须是必要的,如果只有一个属性满足条件,则无法返回。

谢谢大家,我会在评论中尝试一些建议的方法,然后我会发布结果

更新

我现在提供了一个简短的版本,并且根据每个人的评论更具可读性,直到我可以尝试每种方法。编辑以根据@Alexander Powolozki 的回答组合所有表达式。
    public bool IsProductionReadyToStart()
{
bool isValid = true;

isValid &= !IsComponentsRequired || ValidatedComponents;
isValid &= !IsGeometriesRequired || ValidatedGeometries;
isValid &= !IsPokayokesRequired || ValidatedComponents;
isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;

return isValid;
}

最佳答案

该方法的正确实现应该如下所示:

public bool IsProductionReadyToStart()
{
bool isValid = true;

isValid &= !IsComponentsRequired || ValidatedComponents;
isValid &= !IsGeometriesRequired || ValidatedGeometries;
isValid &= !IsPokayokesRequired || ValidatedPokayokes;
isValid &= !IsTechnicalFileRequired || ValidatedTechnicalFile;
isValid &= !IsStandardOperationSheetRequired || ValidatedStandardOperationSheet;
isValid &= !IsOperationMethodRequired || ValidatedOperationMethod;

return isValid;
}
当不使用 &= 时,您会删除所有之前检查的结果,而不是合并它们。

关于C#如何缩短多个If表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62039177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com