Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Equaliy Operator == is not working in Universal Windows

What I have tried:

Am trying to compare two objects of value as keyvaluepair using equality operator . The condition was set true in WPF and get false in Universal Windows.But using equal() get true in both WPF and UWP. Is the equality operator behave differ for WPF and UWP platform? can anyone please suggest you ideas?

public partial class MainWindow : Window
    {
        public List<KeyValuePair<int, double>> Collection { get; set; }
        public MainWindow()
        {           
            InitializeComponent();                      
            Collection = new List<KeyValuePair<int, double>>();
            Collection.Add(new KeyValuePair<int, double>(1, 18));
            Collection.Add(new KeyValuePair<int, double>(2, 36));
            Collection.Add(new KeyValuePair<int, double>(3, 24));
            Collection.Add(new KeyValuePair<int, double>(4, 42));
            Collection.Add(new KeyValuePair<int, double>(5, 30));
            List<object> actualdata = new List<object>();
            for (int c = 0; c < Collection.Count; c++)
            {
                actualdata.Add(Collection[c]);
            }           
            bool flag = false;
            Model modal = new Model();
            modal.Item = actualdata[1];
            object ob = actualdata[1];
            if (modal.Item == ob)
                flag = true;
            else if (ob.Equals(ob))
                flag = true;
            else
                flag = false;

        }
    }
    public class Model : DependencyObject, INotifyPropertyChanged
    {
        public int xval { get; set; }
        public object obj { get; set; }

        public object Item
        {
            get { return (object) GetValue(ItemProperty); }
            set { SetValue(ItemProperty, value); }
        }
        public static readonly DependencyProperty ItemProperty =
            DependencyProperty.Register("Item", typeof(object), typeof(Model), new PropertyMetadata(null, OnPropertyChanged));
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Model).onPropertyChanged("Item");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void onPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs("Item"));
            }
        }
    }

This is my requirement. eventhough the values are same the equlity Operator return true in WPF , false in UWP. Here, i have compared variable of type object with dependency property of type object. i get confused is the dependency property refer any reference in UWP?
Posted
Updated 27-Feb-17 1:38am
v2
Comments
Garth J Lancaster 14-Feb-17 0:24am    
you might like to edit/update your question and show some code - that way it might make sense to anyone who attempts to provide a response
OriginalGriff 14-Feb-17 2:19am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work from.
So copy and paste the relevant code fragment(s), and tell us exactly which line(s) show the problem.
Use the "Improve question" widget to edit your question and provide better information.

1 solution

Hi All,

This below code is my actual requirement.
public partial class MainWindow : Window
   {
       public List<KeyValuePair<int, double>> Collection { get; set; }
       public MainWindow()
       {
           InitializeComponent();
           Collection = new List<KeyValuePair<int, double>>();
           Collection.Add(new KeyValuePair<int, double>(1, 18));
           Collection.Add(new KeyValuePair<int, double>(2, 36));
           Collection.Add(new KeyValuePair<int, double>(3, 24));
           Collection.Add(new KeyValuePair<int, double>(4, 42));
           Collection.Add(new KeyValuePair<int, double>(5, 30));
           List<object> actualdata = new List<object>();
           for (int c = 0; c < Collection.Count; c++)
           {
               actualdata.Add(Collection[c]);
           }
           bool flag = false;
           Model modal = new Model();
           modal.Item = actualdata[1];
           object ob = actualdata[1];
           if (modal.Item == ob)
               flag = true;
           else if (ob.Equals(ob))
               flag = true;
           else
               flag = false;

       }
   }
   public class Model : DependencyObject, INotifyPropertyChanged
   {
       public int xval { get; set; }
       public object obj { get; set; }

       public object Item
       {
           get { return (object) GetValue(ItemProperty); }
           set { SetValue(ItemProperty, value); }
       }
       public static readonly DependencyProperty ItemProperty =
           DependencyProperty.Register("Item", typeof(object), typeof(Model), new PropertyMetadata(null, OnPropertyChanged));
       private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       {
           (d as Model).onPropertyChanged("Item");
       }

       public event PropertyChangedEventHandler PropertyChanged;
       protected virtual void onPropertyChanged(string name)
       {
           PropertyChangedEventHandler handler = PropertyChanged;
           if (handler != null)
           {
               handler(this, new PropertyChangedEventArgs("Item"));
           }
       }
   }




When am trying to run the sample , the equal operator return true in WPF and return false in UWP. Can anyone please share you ideas?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900