0

I have a model consisting of specifications. Each property is a string that I'm converting to an integer for comparison. In the case of there being a null comparison it defaults to false where I want it to be ignored or default to true. Each of the properties needs to be compared to each other and the solution I've come up with is messy. I'm looking for a more readable/reusable solution.

The exception is getting pushed to a grid for the user to see.

    /// <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"); }
            }
        }
    }
Matt
  • 500
  • 7
  • 18
  • 1
    [stringly typed](https://blog.codinghorror.com/new-programming-jargon/)... If you need nullable int why not to use nullable int? – Alexei Levenkov Dec 13 '16 at 18:53
  • Sometimes the spec will actually be a string which is also displayed. I'm only doing the comparison when the property can be converted to an integer. – Matt Dec 13 '16 at 18:54
  • LEL property could be "5" or "Test Result" – Matt Dec 13 '16 at 18:56
  • @Matt See this article... this is a bit more elegant to determine if a string is numeric: http://stackoverflow.com/questions/894263/how-do-i-identify-if-a-string-is-a-number – dvsoukup Dec 13 '16 at 19:18

2 Answers2

1

Refactoring your class might help. By making the private fields/properties integers and the public ones string(if necessary), you can handle any conversions you need in the set method. Also you can keep track of the lowest valued spec, then you only need to check the new value against that. Depending on how reliably typed the data is you can add checks for compliance as well. For example:

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

The LEL property could be this simple:

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"); }
    }
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
1

First I would get all my strings into an int array somewhere else in code;

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

Then your property can look like this:

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

Also, in the two examples you've given, one where you're evaluating if value > _OTHER and an example of how you'd like it to look where you're evaluating if value < _OTHER, when value == _OTHER is handled differently between them. Unless your system somehow does not experience situations where there will be equality, you'll want to make sure you're handling it.

hawkeyegold
  • 129
  • 5