How do I get back the properties of my object in a combo box?

  • Thread starter Thread starter james
  • Start date Start date
J

james

I declared a class with 4 string properties, one of which overrides the
"toString()" method.
I add objects based on this to a combobox, and I can see the properties if I
pause execution and look at the "SelectedItem" I can see my
properties/values in the debugger, but if I try it in the code I can only
see the Equals, GetHashCode, GetType and ToString methods.

How can I access the other properties I defined?

Ta,
James.
 
cast the SelectedItem back to your known type:

MyClass myObj = combo.SelectedItem as MyClass;
if(myObj!=null) {
// do something interesting
}

Marc
 
james said:
I declared a class with 4 string properties, one of which overrides the
"toString()" method.
I add objects based on this to a combobox, and I can see the properties if I
pause execution and look at the "SelectedItem" I can see my
properties/values in the debugger, but if I try it in the code I can only
see the Equals, GetHashCode, GetType and ToString methods.

How can I access the other properties I defined?

You need to typecast the SelectedItem value to your class:

((YourClassNameHere) comboBox.SelectedItem).YourPropertyHere

or

YourClassNameHere value = (YourClassNameHere) comboBox.SelectedItem;
// work with value

-- Barry
 
Barry Kelly said:
You need to typecast the SelectedItem value to your class:

((YourClassNameHere) comboBox.SelectedItem).YourPropertyHere

or

YourClassNameHere value = (YourClassNameHere) comboBox.SelectedItem;
// work with value


Thanks to both of you, working now!
James.
 

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

Back
Top