On 2007-11-30 14:03:41 -0800, "RSH" <wa************ *@yahoo.comsaid :
[...]
But since I dont know the number of departments the system will ultimately
contain, I dont want to hardcode the references in a conditional.
It's difficult for me to be sure I completely understand the question.
I don't find the description very organized. But, that said...
It seems to me that what you've got are already-existing instances of
ApprovalChain-derived classes that are somehow related to your
instantiation of the Purchase class. It appears you may also have a
class named "Budget" that also has derived versions you're using.
Given that, you may want to consider just storing those instances in a
Dictionary<T>, so that you can easily look them up according to their
enum value. For example:
enum Department
{
Acct, Mktg
}
struct DepartmentObjec ts
{
public readonly ApprovalChain ApprovalChain;
public readonly Budget Budget;
public DepartmentObjec ts(ApprovalChai n ApprovalChain, Budget Budget)
{
this.ApprovalCh ain = ApprovalChain;
this.Budget = Budget;
}
}
static Dictionary<Depa rtment, DepartmentObjec tsdictDepartmen ts;
// run once somewhere (e.g. static constructor for whatever class
// contains the "dictDepartment s" object)
foreach (Department dept in Department.GetV alues())
{
switch (dept)
{
case Acct:
dictDepartments .Add(dept, new DepartmentObjec ts(new
AccountingAppro valChain(), new AccountingBudge t()));
break;
case Mktg:
dictDepartments .Add(dept, new DepartmentObjec ts(new
MarketingApprov alChain(), new MarketingBudget ()));
break;
}
}
// then wherever you need to match the enum to an instance, something
// like this:
Department dept = // initialized however...;
ApprovalChain approval = dictDepartments[dept].ApprovalChain;
// Or you could do this:
DepartmentObjec t do = dictDepartments[dept];
Purchase purchase = new Purchase(id, amount, description, dept,
do.ApprovalChai n, do.Budget);
etc.
Note that at some point you do need to hardcode the relationship
between your enum and the class each value represents. Depending on
the actual names you've used for things it certainly is possible to use
reflection to even automate that, but it seems to me it wouldn't really
buy you much. It's not like you can just go adding new ApprovalChain
and Budget objects without visiting the code that uses those objects
and verifying you've done everything right anyway (or at least you
ought to be).
Pete