Click to See Complete Forum and Search --> : Wondering what Object^ selectedItem does.


Riven
November 9th, 2009, 11:02 AM
I was looking at an MSDN example code and it works fine for me but I am not completely sure what all of the lines are doing. I understand how everything works but this one line in the code. Could some one explain what the Object^ line is all about?
int selectedIndex = comboBox1->SelectedIndex;
Object^ selectedItem = comboBox1->SelectedItem;//What does the Object^ part and this line do?
MessageBox::Show( "Selected Item Text: " + selectedItem->ToString() + "\n" +
"Index: " + selectedIndex.ToString() );

Alex F
November 9th, 2009, 11:11 AM
Windows Forms ComboBox is designed by such way, that any Object can be ComboBox item. ComboBox uses item's ToString method to draw an item text. Item may contain additional data. Look at this article:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx
ComboBox.Items Property has ObjectCollection type.

Usually String class is used for ComboBox. If it is necessary to keep additional data in a ComboBox item, you can write class or structure. This is used for the same purpose, as GetItemData or GetItemDataPtr in Win32 programming.