I created a custom
UserControl
ComboBox
m_ComboBox.Items.AddRange((Object[])Enum.GetValues(typeof(Categories)));
ComboBox.SelectedItem
private Object m_LastCategory;
private void ComboBoxSelectedIndexChanged(Object sender, EventArgs e)
{
if (m_ComboBox.SelectedItem != m_LastCategory)
DoSomething();
m_LastCategory = m_ComboBox.SelectedItem;
}
m_LastCategory
SelectedItem
true
Object
if (m_ComboBox.SelectedItem.ToString() != m_LastCategory.ToString())
I know, I could simply cast both variables back to enum and then compare them getting the right result.
Do that. Object equality by default is reference equality, i.e. are those two references pointing to the same location in memory. Two boxed enums are two objects at two different locations in memory, which is why they are always unequal regardless of the value they contain.
In addition, it's in general a good idea to retain as much type information as you can; this lets the compiler help you write correct programs. Typing your m_LastCategory
variable as Object
works against this principle.