The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Help me with Properties! RRS feed

  • Question

  • I've an abstract class consisting of 3 properties like below:

            public IFilter DefaultFilter
            {
                get;
                protected set;
            }
    
            public IFilter PreviousFilter
            {
                get;
                protected set;
            }
    
            public IFilter CurrentFilter
            {
                get;
                protected set;
            }

    I'm assigning default values to all the properties in a class (with above class as base).

    var filter = new BrightnessFilter();
    filter.Level = 0.5;
    
    DefaultFilter = filter;
    CurrentFilter = filter;
    PreviousFilter = filter;

    Now user has given an option to change the value of CurrentFilter using a slider.

    protected void brightnessSlider_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
    {
        CurrentFilter.Level = e.NewValue;
    }

    My issue is when user changes the Current filter level, other(Default & Previous) filter levels are also changing. Why?

    How should I prevent this?


    Clipr : Best ever clipping tool in Windows Phone!
    Download here




    • Edited by venu238 Monday, December 15, 2014 5:23 PM
    Monday, December 15, 2014 5:07 PM

Answers

  • You're setting all three to the same instance of the filter. Since they all reference the same object, changes to one will change all.

    If you want them to be different then you need to set them to different objects:

    var filter = new BrightnessFilter();
    filter.Level = 0.5;
    DefaultFilter = filter;
    
    filter = new BrightnessFilter();
    filter.Level = 0.5;
    CurrentFilter = filter;
    
    filter = new BrightnessFilter();
    filter.Level = 0.5;
    PreviousFilter = filter;

    • Marked as answer by venu238 Friday, December 19, 2014 3:20 AM
    Monday, December 15, 2014 8:32 PM

All replies

  • You're setting all three to the same instance of the filter. Since they all reference the same object, changes to one will change all.

    If you want them to be different then you need to set them to different objects:

    var filter = new BrightnessFilter();
    filter.Level = 0.5;
    DefaultFilter = filter;
    
    filter = new BrightnessFilter();
    filter.Level = 0.5;
    CurrentFilter = filter;
    
    filter = new BrightnessFilter();
    filter.Level = 0.5;
    PreviousFilter = filter;

    • Marked as answer by venu238 Friday, December 19, 2014 3:20 AM
    Monday, December 15, 2014 8:32 PM
  • Now I got it! I've a situation where I've to equate CurrentFilter & PreviousFilter, then again user has an option to change the value of CurrentFilter, when he does that PreviousFilter value is also changing. So how to solve this?

    var previousFilter = new BrightnessFilter();
    previousFilter.Level = CurrentFilter.Level;
    PreviousFilter = previousFilter;


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, December 16, 2014 7:56 AM
  • Same problem, as what pointed by Rob. Consider to have a copy constructor like below

    //Set the value of previousFilter.Level in constructor.

    var previousFilter = new BrightnessFilter(CurrentFilter);


    Tuesday, December 16, 2014 9:46 AM
  • Thank you for the reply!

    I can't really set the value of previousFilter.Level in constructor. Is there any other alternative?


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Tuesday, December 16, 2014 2:13 PM
  • Why not?

    You'll need to provide more details about your specific scenario if you want alternatives that match it.

    --Rob

    Tuesday, December 16, 2014 6:40 PM
  • First I load a list of filters during which I set the value of "DefaultFilter" for each filter.

    When user tap's on a filter, I attach a control to the page while doing that I check whether the same filter is applied previously and thus define "CurrentFilter", "PreviousFilter".

    The control has 3 buttons (apply, reset, remove) and a slider.

    When "apply button" is clicked, the "CurrentFilter" is applied to the current layer(Image), on clicking "reset button" I set "CurrentFilter" as "DefaultFilter", on clicking "remove button" IF filter applied previously I set "CurrentFilter" as "PreviousFilter", ELSE "CurrentFilter" is removed from the current layer.

    AbstractFilter Class

    public abstract partial class AbstractFilter
        {
            public bool IsFilterAppliedPreviously { get; set; }
    
            public LayerControl CurrentLayer { get; set; }
    
            public IFilter DefaultFilter { get; set; }
            public IFilter PreviousFilter { get; set; }
            public IFilter CurrentFilter { get; set; }
    
            public FilterPropertiesControl Control { get; set; }
    
            protected AbstractFilter()
            {
                IsFilterAppliedPreviously = false;
            }
    
            public void CheckForFilter()
            {
                foreach (IFilter filter in CurrentLayer.Filters)
                {
                    if (filter.GetType().Equals(CurrentFilter.GetType()))
                    {
                        IsFilterAppliedPreviously = true;
                        CurrentFilter = filter;
    
                        var previousFilter = CurrentFilter;
                        PreviousFilter = previousFilter;
                        break;
                    }
                }
    
                if (!IsFilterAppliedPreviously)
                {
                    IsFilterAppliedPreviously = false;
                    var filter = DefaultFilter;
                    CurrentFilter = filter;
                    PreviousFilter = filter;
                    CurrentLayer.Filters.Add(CurrentFilter);
                }
            }
    
            public virtual bool AttachControl(FilterPropertiesControl control)
            {
                return false;
            }
    
            public void RemoveFilterFromList()
            {
                foreach (var filter in CurrentLayer.Filters)
                {
                    if (filter.GetType().Equals(CurrentFilter.GetType()))
                    {
                        CurrentLayer.Filters.Remove(filter);
                        break;
                    }
                }
                IsFilterAppliedPreviously = false;
            }
    
            public void ApplyFilter(object sender, System.Windows.RoutedEventArgs e) 
            {
                Control.NotifyManipulated();
    
                Grid grid = (Grid)Control.Parent;
                grid.Children.Remove(Control);
            }
    
            public void RemoveFilter(object sender, System.Windows.RoutedEventArgs e) 
            {
                if (IsFilterAppliedPreviously)
                {
                    var previousFilter = PreviousFilter;
                    CurrentFilter = previousFilter;
                }
                else
                {
                    RemoveFilterFromList();
                }
                    
    
                Control.NotifyManipulated();
    
                Grid grid = (Grid)Control.Parent;
                grid.Children.Remove(Control);
            }
    
            public void ResetFilter()
            {
                var defaultFilter = DefaultFilter;
                CurrentFilter = defaultFilter;
                PreviousFilter = defaultFilter;
                IsFilterAppliedPreviously = false;
            }
        }

    BrightnessFilter

    public partial class BrightnessFilltr : AbstractFilter
        {
            // Constants
            private const double defaultBrightness = 0.0;
    
            // Members
            protected BrightnessFilter _currentFilter;
    
            public BrightnessFilltr()
                : base()
            {
                var defaultfilter = new BrightnessFilter();
                defaultfilter.Level = defaultBrightness;
                DefaultFilter = defaultfilter;
    
                _currentFilter = new BrightnessFilter();
            }
    
            public override bool AttachControl(FilterPropertiesControl control)
            {
                Control = control;
    
                this.CheckforFilter();
    
                Slider brightnessSlider = new Slider();
                brightnessSlider.Minimum = -0.7;
                brightnessSlider.Maximum = 0.7;
                brightnessSlider.Value = _currentFilter.Level;
                brightnessSlider.ValueChanged += brightnessSlider_ValueChanged;
    
                control.ControlsContainer.Children.Add(brightnessSlider);
    
                control.ApplyFilter.Click += ApplyFilter;
                control.ResetFilter.Click += (send, even) =>
                {
                    brightnessSlider.Value = defaultBrightness;
    
                    ResetFilter();
                    Control.NotifyManipulated();
                };
                control.RemoveFilter.Click += RemoveFilter;
    
                return true;
            }
    
            protected void brightnessSlider_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
            {
                _currentFilter.Level = e.NewValue;
                Control.NotifyManipulated();
            }


    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Wednesday, December 17, 2014 4:08 AM
  • And what is going wrong? How does the expected behaviour differ from the actual behaviour?

    You need to clearly explain what you need help with. Several pages of context-free code aren't helpful (if it's more than a few lines to a page of code then uploading a minimal sample project to your OneDrive is much better).

    Friday, December 19, 2014 1:05 AM
  • Sorry for the long codes in my previous post.

    Anyways this below code solved my problem..

    var previousFilter = new BrightnessFilter();
    previousFilter.Level = _currentFilter.Level;
    PreviousFilter = previousFilter;

    Thanks for the support.

    Clipr : Best ever clipping tool in Windows Phone!
    Download here

    Friday, December 19, 2014 3:20 AM