c# scope/typing/assignment question
Posted
by
Shannow
on Stack Overflow
See other posts from Stack Overflow
or by Shannow
Published on 2010-12-21T15:16:45Z
Indexed on
2010/12/22
9:54 UTC
Read the original article
Hit count: 374
Hi there another quick question.
I would like to create a variable object so that depending on the value of something, it gets cast as needed. e.g.
var rule;
switch (seqRuleObj.RuleType) {
case SeqRuleObj.type.Pre :
rule = new preConditionRuleType();
rule = (preConditionRuleType)seqRuleObj.Rule;
break;
case SeqRuleObj.type.Post :
rule = new postConditionRuleType();
rule = (postConditionRuleType)seqRuleObj.Rule;
break;
case SeqRuleObj.type.Exit :
rule = new exitConditionRuleType();
rule = (exitConditionRuleType)seqRuleObj.Rule;
break;
default :
break;
}
String result;
foreach (sequencingRuleTypeRuleConditionsRuleCondition cond in rule.ruleConditions.ruleCondition) {
....../ blah
}
so basically this will not work. c# will not allow me to create an new object in every case as the name is aleady defined.
i can just paste the foreach loop into each case but that to me is such a waste, as the objects are exactly the same in all but name.
© Stack Overflow or respective owner