P0lT10n P0lT10n - 9 days ago 6
ASP.NET (C#) Question

Dynamically set Type when calling constructor with predefined types

I am currently struggling to create a public attribute but the type is defined when constructor is called.

This is my code now:


public class clsHtml: IDisposable
{

public enum Types { Section, Input, File, Radio, Checkbox, Select };

public TYPE HERE element;

public clsHtml(Types type)
{
CreateElement(type);
}

private void CreateElement(Types type)
{
switch(type)
{
case Types.Input:
element = new HtmlInputText();
break;

case Types.File:
element = new HtmlInputFile();
break;

case Types.Radio:
element = new HtmlInputRadioButton();
break;

case Types.Checkbox:
element = new HtmlInputCheckBox();
break;

case Types.Select:
element = new HtmlSelect();
break;
}
}

public override string ToString()
{
System.IO.StringWriter writer = new System.IO.StringWriter();
HtmlTextWriter html = new HtmlTextWriter(writer);

element.RenderControl(html);

return writer.ToString();
}
}


Where it says
TYPE HERE
, should I use
dynamic
or
object
or what ? I am going to access it from outside (public), I just have a simple
ToString()
method that will parse it with
StringWriter
and
HtmlTextWriter
to get it as HTML String.

I am using ASP.NET (not MVC) so, is it a performance matter what I choose ? If I choose
object
type, can I still access the methods that are on each object inside the
switch
?

I want to do a
new
on this class defining the type of element it will create, so I can use some methods of the selected class to set things. For example, if I choose
HtmlInputRadioButton
I can use
.Checked
but I will use this element in other class, for setting attributes depending which element you created. Am I clear ?




One possible solution is to create variables with each type, but use a property to see which one is not null and get that one but this is not an elegant solution.




Other solution is to use Generics using
where T : HtmlControls, new()
but I want the user to not know that, I want him to only write
new(Types.WHICH ONE TO USE)
.

Thanks all !!!

Answer
public enum Types { Section, Input, File, Radio, Checkbox, Select };

// Why did you decide it to be IDisposable? What are you going to dispose?
public class clsHtml: IDisposable
{
    public HtmlControl element { get; private set; }

    public clsHtml(Types type)
    {
        this.CreateElement(type);
    }

    private void CreateElement(Types type)
    {
        switch(type)
        {
            case Types.Input:
                this.element = new HtmlInputText();
                break;

            case Types.File:
                this.element = new HtmlInputFile();
                break;

            case Types.Radio:
                this.element = new HtmlInputRadioButton();
                break;

            case Types.Checkbox:
                this.element = new HtmlInputCheckBox();
                break;

            case Types.Select:
                this.element = new HtmlSelect();
                break;

            default:
                throw new NotImplementedException(type.ToString() + " not yet supported!");
        }
    }

    public override string ToString()
    {
        System.IO.StringWriter writer = new System.IO.StringWriter();
        HtmlTextWriter html = new HtmlTextWriter(writer);

        this.element.RenderControl(html);

        return writer.ToString();
    }
}

var clsHtml = new clsHtml(Types.Input);

// the following part sucks, but you don't want to use generics
if (clsHtml.element is HtmlInputText)
{
  HtmlInputText elementAsHtmlInputText = clsHtml.element as HtmlInputText;
}
else if ( ...)
...
Comments