Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
Can you please add the code where you are consuming the results of the switch statement? – Shai Cohen Sep 20 '12 at 17:23

2 Answers

up vote 1 down vote accepted

If you are working with .net 4.0framework you can use a dynamic variable

dynamic 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;
    }

After this you can get its type of control .. or you can use directly like

string boo = txt.Text;

or if you want to used as checkbox

you can use

bool checked = txt.Checked;

for read about dynamic variable go http://msdn.microsoft.com/en-us/library/dd264736.aspx

share|improve this answer
1  
Thanks! Dynamic variables are a new one on my and fortuneately working with .NET 4.0 - You do need a level of bravery around them clearly!! – RemarkLima Sep 20 '12 at 20:25

You can modify object txt with WebControl txt in order to access common properties

And you cast to specific type if you wish adjust specific properties.

    //You declare as global variable of your class        
    public WebControl control{get;set};

    ....
    //Your code inside method
    switch (_inputType)
    {
        case code.enums.inputType.textBox:
            control= new TextBox();
            //Here you can set global property
            control.Id = ""; //etc.
            break;
        case code.enums.inputType.dropDownList:
            control= new DropDownList();
            break;
        case code.enums.inputType.checkBox:
            control = new CheckBox();
            break;
    }


//For Specific property

var test = (TextBox)control;
//Add specific property.
share|improve this answer
How would I access result outside of the switch statement? – RemarkLima Sep 20 '12 at 17:44
you declare you control outside of swith, as global variable and you access , but if you wish accee properties you cast your property – Aghilas Yakoub Sep 20 '12 at 17:46
I do like this, and the option of having a public property meaning it could be access from outside the UserControl could also prove useful. Thank you. – RemarkLima Sep 20 '12 at 20:27
I'am happy to help you RemarkLima – Aghilas Yakoub Sep 20 '12 at 23:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.