473,246 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 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 1352
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
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
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
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
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
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
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
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
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
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
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.