471,326 Members | 1,881 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,326 software developers and data experts.

Generic function to output the values of an Enumeration

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();
}
Jun 27 '08 #1
3 1290
On 13 maio, 13:19, DotNetNewbie <snowman908...@yahoo.comwrote:
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.
Jun 27 '08 #2
On Tue, 13 May 2008 09:19:57 -0700, DotNetNewbie <sn***********@yahoo.com>
wrote:
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.
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
Jun 27 '08 #3
On May 13, 3:11*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Tue, 13 May 2008 09:19:57 -0700, DotNetNewbie <snowman908...@yahoo.com>*
wrote:
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.

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!
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Codex Twin | last post: by
9 posts views Thread by Satish Itty | last post: by
26 posts views Thread by raylopez99 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.