HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Better method for my property Checking, replacement of switch statement

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
methodstatementcheckingbetterpropertyforswitchreplacement

Problem

I'm hoping this is the correct place for this, I'm struggling a little for wording as i'm not sure what you would call this.

Basically, i have a system in place where my datagrid marks cells that have changed with a new background colour, to do this i have a method in the object that contains these properties that receives a string which is the name of the property to check, and then a switch statement that takes that string to check the correct property.

public Color HasChanged(string value)
    {
        switch (value)
        {
            case "CILRef":
                if (_localShipment.cilRef != _originalShipment.cilRef)
                {
                    return Colors.SkyBlue;
                }
                else
                {
                    return Colors.White;
                }
            case "ArrivedAtPortDate":
                if (_localShipment.arrivedAtPortDate != _originalShipment.arrivedAtPortDate)
                {
                    return Colors.SkyBlue;
                }
                else
                {
                    return Colors.White;
                }
  }


I've removed the rest of the properties for brevity.

Now i get the nagging sensation that there is a cleaner way to do this string>property without using a switch statement, but i can't for the life of me find anything on google, it's hard to search without some keyword to go on.

I'm also attempting to only save those properties that have changed, i was going to place any changed property name into an array, and then have a loop with yet another switch statement that checked that array and then saved the correct property. However this again seems untidy to me.

is there a cleaner solution to this, hopefully that could handle the addition of new properties without needing to add new code to the switch statements.

I can include the rest of the code that does this checking (namely the WPF binding on the datagrid, and a converter that calls the checking

Solution

Ok, so having spoken to others on stackoverflow, i have change the code, this (i hope) satisfies all the issue's I've pointed out and all the issues pointed out to me.

The HasChanged is now:

public Color HasChanged(string value)
{
    try
    {
        var data1 = _localShipment.GetType().GetProperty(value, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(_localShipment, null);
        var data2 = _originalShipment.GetType().GetProperty(value, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(_originalShipment, null);
        return data1 != data2 ? Colors.SkyBlue : Colors.White;
    }
    catch (Exception ex)
    {
        return Colors.White;
    } 
}


EDIT:

This has been changed to the following methods:

private object GetPropValue(object src, string propName)
    {
            PropertyInfo p = src.GetType().GetProperty(propName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            string value = (string)p.GetValue(src, null);
            return value;
    }

    public bool HasChanged(string value)
    {
        var data1 = GetPropValue(_localShipment, value);
        var data2 = GetPropValue(_originalShipment, value);
        return data1 != data2 ? true : false;
    }
    #endregion
}


and the converter has been changed to :

class HasChangedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) { return Colors.White; }
        var shipment = value as Shipment;
        var property = parameter as string;
        if (shipment.HasChanged(property)) { return Colors.SkyBlue; } else { return Colors.White; }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}


This solves the issues mentioned previously in this question.

Code Snippets

public Color HasChanged(string value)
{
    try
    {
        var data1 = _localShipment.GetType().GetProperty(value, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(_localShipment, null);
        var data2 = _originalShipment.GetType().GetProperty(value, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(_originalShipment, null);
        return data1 != data2 ? Colors.SkyBlue : Colors.White;
    }
    catch (Exception ex)
    {
        return Colors.White;
    } 
}
private object GetPropValue(object src, string propName)
    {
            PropertyInfo p = src.GetType().GetProperty(propName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            string value = (string)p.GetValue(src, null);
            return value;
    }

    public bool HasChanged(string value)
    {
        var data1 = GetPropValue(_localShipment, value);
        var data2 = GetPropValue(_originalShipment, value);
        return data1 != data2 ? true : false;
    }
    #endregion
}
class HasChangedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null) { return Colors.White; }
        var shipment = value as Shipment;
        var property = parameter as string;
        if (shipment.HasChanged(property)) { return Colors.SkyBlue; } else { return Colors.White; }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}

Context

StackExchange Code Review Q#43495, answer score: 4

Revisions (0)

No revisions yet.