Generic function to output the values of an Enumeration

D

DotNetNewbie

Hi,
I want to create a method that takes in the type of a Enumeration,
passes in the Enumeration as an object, and then outputs the values
with checkboxes, and pre-checks the checkbox if that value is set in a
mask.

This is what I have so far that outputs the values as a checkbox, but
I need to pass in *any* mask so it can check if that value is set in
the mask, if yes, check the checkbox.

public static string EnumToCheckboxes(Type enumType, string
namePrefix)
{
StringBuilder sb = new StringBuilder();


foreach (string item in Enum.GetNames(enumType))
{
string name = String.Format("{0}-{1}", namePrefix,
item);

sb.Append(@"<li><input type=""checkbox""
name=""").Append(name).Append(@"""").Append(@"
id=""").Append(name).Append(@"""></li>");
sb.Append(item);
}


return sb.ToString();
}


What i'm thinking to do is:

public static string EnumToCheckboxes(Type enumType, string
namePrefix, object mask)
{

StringBuilder sb = new StringBuilder();

(enumType)mask // ????????????????

foreach (string item in Enum.GetNames(enumType))
{
string name = String.Format("{0}-{1}", namePrefix,
item);

sb.Append(@"<li><input type=""checkbox""
name=""").Append(name).Append(@"""").Append(@"
id=""").Append(name).Append(@"""></li>");
sb.Append(item);
}




return sb.ToString();
}
 
B

Breno P. Lucena

Hi,
I want to create a method that takes in the type of a Enumeration,
passes in the Enumeration as an object, and then outputs the values
with checkboxes, and pre-checks the checkbox if that value is set in a
mask.

This is what I have so far that outputs the values as a checkbox, but
I need to pass in *any* mask so it can check if that value is set in
the mask, if yes, check the checkbox.

public static string EnumToCheckboxes(Type enumType, string
namePrefix)
        {
            StringBuilder sb = new StringBuilder();

            foreach (string item in Enum.GetNames(enumType))
            {
                string name = String.Format("{0}-{1}", namePrefix,
item);

                sb.Append(@"<li><input type=""checkbox""
name=""").Append(name).Append(@"""").Append(@"
id=""").Append(name).Append(@"""></li>");
                sb.Append(item);
            }

            return sb.ToString();
        }

What i'm thinking to do is:

public static string EnumToCheckboxes(Type enumType, string
namePrefix, object mask)
        {

            StringBuilder sb = new StringBuilder();

            (enumType)mask // ????????????????

            foreach (string item in Enum.GetNames(enumType))
            {
                string name = String.Format("{0}-{1}", namePrefix,
item);

                sb.Append(@"<li><input type=""checkbox""
name=""").Append(name).Append(@"""").Append(@"
id=""").Append(name).Append(@"""></li>");
                sb.Append(item);
            }

            return sb.ToString();
        }


Well, you´ve to learn a few thing about reflection.
Try it, it will probably help you.

Best Regards,

Breno.
 
D

DotNetNewbie

What I would do is enumerate the Enum.GetValues() array instead of the  
GetNames() array.  Cast your mask and current value to int, and set the  
checkbox based on that result.

You could of course parse the enumeration value name, going the other  
direction.  But I think it reads better starting with the value, and I  
think that converting the value to a string (for use as the "name") is  
slightly easier than converting the string to a value (though neither is  
really that hard).

Pete

That worked great, thanks Pete!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top