creating objects without explicitly declaring them

fguihen

Junior Contributor
Joined
Nov 10, 2003
Messages
248
Location
Eire
doing a little project. im demonstrating inheritance.
i have an employee class.
classes which inherit this are BusinessEmp, MaintenanceEmp, ITEmp.......

i have a form to create employees. a dropdown box lets me select the employee type.
rather than hardcoding it

if(combo.selectedItem = "businessEmp")
{
//create new businessEmp
}
if(combo.selectedItem = "ITEmp"()
{
//create new ITEmp
}
...
...

i want it to be extensible, so that if someone added a different type of employee, all they would have to do is add in the new employee class. is this possible ( cant use a database)?i can hard code them if i want, but id rather learn to do it this way, seems like there would be more merrit to it
 
The way I would do it is rather than inheriting from a base class use an Interface, then you create each employee type as a plugin (theres an excellent plugin example in the tutors corner). You could probably still do the plugin approach using a base class, but as i've never attempted this and as such I couldn't say much more.

The basic idea is that a plugin controller will load a list of each available employee type. You can then use the SelectedIndex value of the combobox to create an object of that type.
 
If I can dig it up I do have a sample knocking around somewhere (a continuation of the tutorials I will get round to finishing in the tutor's corner).

It's a VB sample that dynamically loads a list of available plugins that the UI can use to display to the user and a factory that then creates the correct instance internally.

Not too difficult to do either, getting the plugins designed correctly is the biggest challenge.
 
Back
Top