searching a Combo box for a Generic item

C

Chuck P

I have a DisplayValueItem object that is used to fill a ComboListBox
with items. It used to be the Value and Display members were string.
I changed it to a generic (T, string) and now the below code fails.

x = new DisplayValueItem < int >(1, "stuff");
clbOutcomes.Items.Add(x);
bFound = clbOutcomes.Items.Contains(x);
bFound = clbOutcomes.Items.Contains(new DisplayValueItem < int >(1,
"stuff"));

bFound for x comes out true
bFound for new comes out false

I don't understand why it worked before I turned the DisplayValueItem
from a non-Generic (string, string) into a Generic (T,string).


public class DisplayValueItem
{
private string mValue;
private string mDisplay;
public DisplayValueItem(string ValueField, string
DisplayField)
{
mValue = ValueField;
mDisplay = DisplayField;
}
public string Value
{
get { return mValue; }
}

public string Display
{
get { return mDisplay; }
}

}


public class DisplayValueItem<T>
{
private T mValue;
private string mDisplay;
public DisplayValueItem(T ValueField, string DisplayField)
{
mValue = ValueField;
mDisplay = DisplayField;
}
public T Value
{
get { return mValue; }
}

public string Display
{
get { return mDisplay; }
}

}
 
W

Walter Wang [MSFT]

Hi Chuck,

I'm not sure what do you mean by "why it worked before I turned the
DisplayValueItem from a non-Generic (string, string) into a Generic (T,
string)" because using your non-Generic version of DisplayValueItem,
following code also turns out the same true and false result:

DisplayValueItem y = new DisplayValueItem("1", "stuff");
comboBox1.Items.Add(y);
bFound = comboBox1.Items.Contains(y);
bFound = comboBox1.Items.Contains(new DisplayValueItem("1", "stuff"));

Which I think is expected because you didn't override Equals in
DisplayValueItem to compare by the fields in it.

ComboBox internally uses a non-Generic ArrayList to store the items.
ArrayList uses Object.Equals to find an item. If your intention is to find
item by both mValue and mDisplay member in your class DisplayValueItem, you
need to override Equals like following:

public class DisplayValueItem
{
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj.GetType() != this.GetType()) return false;
DisplayValueItem other = obj as DisplayValueItem;
return mValue.Equals(other.mValue) &&
mDisplay.Equals(other.mDisplay);
}
}


public class DisplayValueItem<T>
{
public override bool Equals(object obj)
{
if (obj == null) return false;
if (obj.GetType() != this.GetType()) return false;
DisplayValueItem<T> other = obj as DisplayValueItem<T>;
return mValue.Equals(other.mValue) &&
mDisplay.Equals(other.mDisplay);
}
}

I hope this helps. Please reply to let us know whether or not you need
further information. Thank you.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
C

Chuck

thanks,
maybe it was one of those cases where it was comparing strings on the
stack.

I got a warning about the hashcode so I used this too:

public override int GetHashCode()
{
return mDisplay.GetHashCode() ^ mValue.GetHashCode();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top