473,508 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1362
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1318
by: Codex Twin | last post by:
hello group: The following is a fragment from a schema which defines the EWethnicCategoryStructure type. As you can see, its type is defined by the SimpleType enumeration EWethnicCategoryType....
3
3318
by: Claire | last post by:
I have a large record with many enumerated fields. The record is stored in a file and the fields have to be extracted. I validate the data as it's read, but there's so many tests similar to the...
2
31648
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
6
4880
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
27
2638
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
1
2045
by: GU | last post by:
How can one get a list of enumeration values for a given element in an XML schema? I have been looking at the Schema Object Model, but I'm making very little headway. Can anyone give me a nudge...
1
1980
by: JavaXMLDev | last post by:
Hi I'm new to XML schema work, but wondered how, if at all possible, can you specify a relationship between the values of an element's attributes. Essentially I want context sensitive...
9
7574
by: Satish Itty | last post by:
How do I write the following c# code in vb Product FindProduct(string code) { List<Productproducts = getProducts(); return products.Find(delegate(Product bo) { return bo.Code == code; }); }
26
3591
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7489
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.