Cast object to WebControl based on Switch enum?
<p>I'm not sure if this is possible in C#:</p>

<p>I have a <code>userControl</code> in which I have a property <code>inputType</code> - enum'd to have 3 possible options, textBox, dropDownList or checkBox.</p>

<p>I'd then like to create a new object of that type and add it to a placeholder in the userControl:</p>

<p>I can do this:</p>

<pre><code>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;
    }
</code></pre>

<p>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 - <code>(TextBox)txt</code> -as I don't know which one it'll be...</p>

<p>Is there a way to do this? Or should I look to do all control specific operations in the <code>switch</code>?</p>

<p>As requested, ideally I'd like the <code>object txt</code> to be available for the rest of the page. So after the switch statement to be able to access <code>txt</code>.</p>

<p>I'll definately want to do:</p>

<pre><code>plcHolder.Controls.add(txt);
</code></pre>

<p>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 :)</p>

<p>Many thanks,
Mark</p>
