RSH wrote:
I am slowly getting the hang of objects and creating my own. I was given
some help in assigning an object to a ComboBox collection. I have been
looking at it and I get the concept which is very powerful but I'm having a
bit of a problem conceptually. I was hoping someone might be able to shed
some light on creating this object and assigning it in the manner. Also how
would I reference this object by name after it is created in this manner?
Is there a collection of objects that I could iterate through with respect
to this ComboBox?
[snip code]
Objects don't necessarily have names, and collections don't necessarily
support getting contained members by name. Of course, there's always
_some_ way to 'get at' them. You can see that here in the case of these
Employee objects that you put in the ComboBox. See the line where you
put them in:
ComboList.Items.Add(new Employee(sTemp, DR));
that tells you that ComboBox ComboList has a a collection object called
Items. So you go to the documentation (or just the object browser) and
you see
[C#]
public ComboBox.ObjectCollection Items {get;}
A System.Windows.Forms.ComboBox.ObjectCollection representing the items
in the ComboBox.
Remarks
This property enables you to obtain a reference to the list of items
that are currently stored in the ComboBox. With this reference, you can
add items, remove items, and obtain a count of the items in the
collection. For more information on the tasks that can be performed
with the item collection, see the
System.Windows.Forms.ComboBox.ObjectCollection class reference topics.
So then you go and look at ComboBox.ObjectCollection, to see the ways
you can get objects back out of the collection. And you see that this
class implements IEnumerable, which means you can foreach over the
collection. So that's one way you can get each item back. Another way
is the indexer for the collection - Items[0] is the first member of the
collection, Items[1] is the second, and so on.
--
Larry Lard
Replies to group please