I'm not sure if this is possible in C#:
I have a userControl
in which I have a property inputType
- enum'd to have 3 possible options, textBox, dropDownList or checkBox.
I'd then like to create a new object of that type and add it to a placeholder in the userControl:
I can do this:
object txt;
switch (_inputType)
{
case code.enums.inputType.textBox:
txt = new TextBox();
break;
case code.enums.inputType.dropDownList:
txt = new DropDownList();
break;
case code.enums.inputType.checkBox:
txt = new CheckBox();
break;
}
However, then I can't access the properties of the textbox, dropdownlist or checkbox as Visual Studio doesn't know the type - Nor can I cast the object - (TextBox)txt
-as I don't know which one it'll be...
Is there a way to do this? Or should I look to do all control specific operations in the switch
?
As requested, ideally I'd like the object txt
to be available for the rest of the page. So after the switch statement to be able to access txt
.
I'll definately want to do:
plcHolder.Controls.add(txt);
That part is fine - it's just trying to access the control specific properties before that... Which I think it can't be done... But always best check with the SO experts :)
Many thanks, Mark