7 posts in this topic

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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);
}
}
}[/CODE]

Share this post


Link to post
Share on other sites

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 :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.