Duplicate code in day of the week event handlers private static void OnSundayChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {
        BinaryDataGrid c = sender as BinaryDataGrid;
        if (c != null)
        {

            if (_shouldCallBack)
            {
                if (c.Sunday.Length == 4)
                    return;
                Days day = (Days)c.dataGrid.Items[0];
                byte[] newArray = new byte[c.Sunday.Length + 1];
                newArray[0] = Convert.ToByte(0);
                c.Sunday.CopyTo(newArray, 1);
                day.Value = BitConverter.ToInt32(newArray.Reverse().ToArray(), 0);
                _sun = day.Value;
            }
            else
            {
                _shouldCallBack = true;
                c.Sunday = BitConverter.GetBytes(_sun).Reverse().ToArray().Skip(1).ToArray();
            }
        }
    }

    private static void OnMondayChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e)
    {

        BinaryDataGrid c = sender as BinaryDataGrid;
        if (c != null)
        {
            if (_shouldCallBack)
            {
                if (c.Monday.Length == 4)
                    return;
                Days day = (Days)c.dataGrid.Items[1];
                byte[] newArray = new byte[c.Monday.Length + 1];
                newArray[0] = Convert.ToByte(0);
                c.Monday.CopyTo(newArray, 1);
                day.Value = BitConverter.ToInt32(newArray.Reverse().ToArray(), 0);
                _mon = day.Value;
            }
            else
            {
                _shouldCallBack = true;
                c.Monday = BitConverter.GetBytes(_mon).Reverse().ToArray().Skip(1).ToArray();
            }
        }
    }


I've got the same code for every week day, how can I generalize this so I don't have 6 duplicates of the code above?
What you need to do is:


Add a parameter of type System.DayOfWeek called, say, day_of_week.
Replace the fields BinaryDataGrid.Sunday, .Monday, etc. with a Dictionary&lt;K,V&gt; that has a key of type System.DayOfWeek and use day_of_week to access values within that dictionary.
Do the same for your _sun, _mon etc. global variables. (For which you will burn in hell, incidentally.)


If you cannot modify BinaryDataGrid, then write two helper methods, one for getting and one for setting the value of the day member of BinaryDataGrid: Each method has a switch( day_of_week ) and modifies the appropriate member of BinaryDataGrid.

Good luck and have fun.