Jump to content



Photo

[C#/Win8] Weird results when calling IndexOf

c# windows 8 metro programming win8 modern ui

  • Please log in to reply
6 replies to this topic

#1 sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 16 December 2012 - 03:32

I have never had this issue with calling IndexOf. I have a simple control in a stackpanel and I add it to a list to be sorted later:

ItenGSDefCtrl gsDefCtrl = new ItenGSDefCtrl()
                                {
                                    ...
                                };

this.xItenGSLstStk.Children.Add(gsDefCtrl);
                                this.gl_itenOdrLst.Add(gsDefCtrl);
When I click a button on that control I want it to get the current index in the list for that control.

int cIndex = this.gl_itenOdrLst.IndexOf(sender);
Seems simple right? Well it keeps returning -1 even though I can see the control(s) in the list in the debugger...I've never had this issue before. Did something change in win8 with the way you can retrieve index locations?


#2 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 11
  • Joined: 26-October 05

Posted 16 December 2012 - 03:45

All I can think of is that IndexOf uses EqualityComparer(T).Default to compare values; that is a reference equality comparison by default but can be overriden by implementing IEquatable(T) or overriding Object.Equals(). If you have the definition of "ItenGSDefCtrl" you could check to see how equality is being performed.

#3 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 16 December 2012 - 17:34

Well thanks for the speedy response as always Dr_Asik :) I had thought about overriding Object.Equals() but it just seems like I shouldn't have to. I've always been able to take an object that is raising an event and determine its index location in a list that it belongs in. Apparently that is not the case anymore. I'll override the Equals method and we'll see how it goes!

#4 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 16 December 2012 - 17:47

Oddly enough, when I assign an internal id to the control and look it up when comparing it the ids are different o_O?
Checking to see if the object is the same. Addr of sender: 4618 Test Road, Test, OH 55555 / Addr of this object: 4618 Test Road, Test, OH 55555 | Int guid of sender: e36a6472-4cb5-4a15-b25b-f7157164f46f / Int guid of this object: 5c0de091-1de5-434c-b615-6332c26e560c. But I only call the constructor once and add that object to both the stackpanel and the private global list object. Why would it generate 2 ids when the id is only generated at the constructor? Obviously I could take out the internal id checking and only check the addresses but that is not an option at this point.

#5 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 11
  • Joined: 26-October 05

Posted 16 December 2012 - 21:26

If one of the fields is different then it has to be two different instances. The object must be being copied at some point. Put a breakpoint in the constructor and check what instantiates it. I can't reproduce this with a simple test case:


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

namespace WPFEqualityTest {

    class App {
        static Button s_button;
        static List<Button> s_buttonList = new List<Button>();
            
        [STAThread]
        static void Main() {
            var win = new Window();
            win.Show();
            var sp = new StackPanel();
            win.Content = sp;
            
            s_button = new Button();
            s_button.Click += ButtonOnClick;
            sp.Children.Add(s_button);
            s_buttonList.Add(s_button);


            var app = new Application();
            app.Run();
        }

        static void ButtonOnClick(object sender, RoutedEventArgs routedEventArgs) {
            Debug.Assert(s_buttonList.IndexOf((Button)sender) > -1);
        }
    }
}


#6 OP sathenzar

sathenzar

    Neowinian

  • Joined: 12-June 06

Posted 19 December 2012 - 17:38

Well as usual I appreciate your help Dr_Asik. I'm not sure what I changed but it just started working...obviously I changed something that caused it to work but I have no idea what b/c I only create the control once in one function and never have a function that creates the methods again. Either way it works now so thanks for all of the help :)

#7 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 11
  • Joined: 26-October 05

Posted 19 December 2012 - 21:42

Ugh, the worst type of bugs are those that mysteriously disappear by themselves... let's just knock on wood then!