Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class with in a class

Status
Not open for further replies.

WJProctor

Programmer
Jun 20, 2003
151
GB
Hi there, im having a little problem. I have a class (Box) and a Class (Item). Obviously a box can have items. I would idealy like to be able to say:

Box.Item(1).Name = Item1
Box.Item(2).Name = Item2

And so on. How would i code this, i cant seem to work it out, ive tried all sorts and run out of ideas. Could any body point me in the right direction or tell me where i could find out more about this.

Thanks


James Proctor

 
The easy way is to add a ArrayList or HashTable collection to your box class and use it to hold your Item objects.

The more correct way is to define a new class that inherits from CollectionBase is called Items (plural). It has an Item method (part of the CollectionBase) that returns an Item object (as opposed to the default Object which isn't typesafe). Take a look at how the framework does it -- it defines a Form object that contains an collection object named Controls, which contains items of type Control.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top