当一个为空时,将整数和默认比较与true进行比较

时间:2016-12-13 18:40:20

标签: c# .net wpf mvvm

我有一个由规格组成的模型。每个属性都是一个字符串,我将其转换为整数进行比较。在存在空比较的情况下,默认为false,我希望忽略它或默认为true。每个属性都需要相互比较,我提出的解决方案是混乱的。我正在寻找更具可读性/可重用性的解决方案。

例外情况是被推送到网格供用户查看。

    /// <summary>
    /// Lower Entry Limit of the spec
    /// </summary>
    public string LEL
    {
        get { return _LEL; }
        set
        {
            //This is what I've come up with
            if(value != null)
            {
                if (_LRL != null)
                {
                    if (Utilities.ConvertSafe.ToInt32(value) > (Utilities.ConvertSafe.ToInt32(_LRL))) throw new Exception("LEL not correct");
                }
                if (_LWL != null)
                {
                    if (Utilities.ConvertSafe.ToInt32(value) > (Utilities.ConvertSafe.ToInt32(_LWL))) throw new Exception("LEL not correct");
                }
                 _LEL = value; NotifyPropertyChanged(this, "LEL");
 //-------------------------------------
                //Want something more like this
                if (Utilities.ConvertSafe.ToInt32(value) < (Utilities.ConvertSafe.ToInt32(_LRL)) //If is _LRL is null ignore this comparison
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_LWL)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_LUL)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_Target)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_UUL)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_UWL)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_URL)
               && Utilities.ConvertSafe.ToInt32(value) < Utilities.ConvertSafe.ToInt32(_UEL)
               )
                {
                    _LEL = value; NotifyPropertyChanged(this, "LEL");
                }
                else { throw new Exception("LEL not correct"); }
            }
        }
    }

2 个答案:

答案 0 :(得分:1)

重构你的课程可能有所帮助。通过创建私有字段/属性整数和公共字符串(如果需要),您可以处理set方法中所需的任何转换。您还可以跟踪最低价值的规范,然后您只需要检查新值。根据数据输入的可靠性,您还可以添加检查合规性。例如:

private int _LRL = 0;
private int lowestSpec = Int32.MaxValue;
public string LRL
{
    get{return _LRL.ToString();}
    set
    {
            _LRL = Utilities.ConvertSafe.ToInt32(value);
        if(_LRL < lowestSpec)
        {
            lowestSpec = _LRL;
        }
    }
}

LEL属性​​可能很简单:

public string LEL
{
    get { return _LEL.ToString(); }
    set
    {
        int temp = Utilities.ConvertSafe.ToInt32(value);

        if  (temp < lowestSpec)
        {
            _LEL = temp; 
            NotifyPropertyChanged(this, "LEL");
        }
        else { throw new Exception("LEL not correct"); }
    }
}

答案 1 :(得分:1)

首先,我会将所有字符串放入代码中其他地方的int数组中;

int[] _evalList= new int[] { Utilities.ConvertSafe.ToInt32(_LRL),
    Utilities.ConvertSafe.ToInt32(_LWL),
    Utilities.ConvertSafe.ToInt32(_LUL),
    Utilities.ConvertSafe.ToInt32(_Target),
    Utilities.ConvertSafe.ToInt32(_UUL),
    Utilities.ConvertSafe.ToInt32(_UWL),
    Utilities.ConvertSafe.ToInt32(_URL),
    Utilities.ConvertSafe.ToInt32(_UEL) };

然后你的财产看起来像这样:

    public string LEL
    {
        get { return _LEL; }
        set
        {
            if(value!=null)
            {
                int testVal=Utilities.ConvertSafe.ToInt32(value);
                for(int i = 0;i<_evalList.Length;i++)
                {
                    if(testVal<_evalList[i]) { continue; }
                    throw new Exception("LEL not correct");
                }
                _LEL=value;
                NotifyPropertyChanged(this,"LEL");
            }
        }
    }

此外,在您给出的两个示例中,您要评估的值是&gt; _OTHER以及一个例子,说明您如何评估价值&lt;&lt; _OTHER,当值== _OTHER时,它们之间的处理方式不同。除非你的系统在某种程度上没有遇到平等的情况,否则你要确保你正在处理它。