 |
|
|
 |
|
 |
I have a datagrid which is bound through dataset / tableadapter.
How can I get the enum value there because I do not add columns manually and I cannot call methods there...
Right now the table adapter loads the integer field from the database and puts in the integer value in the grid.
There I would need to tell it is the enum TaskStatus
Greetings,
|
|
|
|
 |
|
 |
How to do internationalization for [Description] tag in enum
|
|
|
|
 |
|
 |
Very helpful, .Net rocks!
|
|
|
|
 |
|
 |
I have created a small prototype using this attribute and trying to improve its performance using different caching methods. This code is available on my blog (in french, but you can download and understand the code in english). The cached results are up to 85 times faster than uncached results, depending on the caching mechanism.
|
|
|
|
 |
|
 |
for posting this simple and elegant solution. It just came in handy now!
Marc
Thyme In The CountryPeople are just notoriously impossible. --DavidCrow There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith
|
|
|
|
 |
|
 |
Is this solution also for C / C++ available ?
Greetings
Jimmy
|
|
|
|
 |
|
|
 |
|
 |
MyEnum.tostring("G") will do the same thing.
|
|
|
|
 |
|
|
 |
|
 |
Nice article. But i get into trouble when i`m using [Sytstem.Flags] attribute and concat values ex:
MyColors cols = MyColors.LightGreen | MyColors.PitchBlack;
Anybody know how to solve this?;)
|
|
|
|
 |
|
 |
Here's an easy way to do it (if you're still interested), since Enum.ToString() will give you the enum names, you can use that to get the descriptions. public static string[] GetFlagDescriptions(Enum value) { string[] names = value.ToString().Split(new string[] { ", " }, StringSplitOptions.None); string[] descriptions = new string[names.Length]; for (int i = 0; i < names.Length; i++) { Enum enumValue = (Enum)Enum.Parse(value.GetType(), names[i]); descriptions[i] = (GetDescription(enumValue)); } return descriptions; } John
|
|
|
|
 |
|
 |
This method attempts to break an enum down into its greatest common values. The return value indicates if the enum was completely covered by the returned enum value list. Perhaps it should be called TryGetEnumValues
private bool GetEnumValues(Type enumType, object value, out IEnumerable<object> enumValues)
{
bool enumCompletelyCovered = false;
List<object> enumValueCollection = new List<object>();
if(Enum.IsDefined(enumType, value))
{
enumValueCollection.Add(value);
enumCompletelyCovered = true;
}
else
{
ulong currentEnumValue = Convert.ToUInt64(value);
Array values = Enum.GetValues(enumType);
int index = values.Length - 1;
while(index >= 0)
{
ulong currentIndexedValue = System.Convert.ToUInt64(values.GetValue(index));
if((index == 0) && (currentIndexedValue == 0L))
{
break;
}
if((currentEnumValue & currentIndexedValue) == currentIndexedValue)
{
currentEnumValue -= currentIndexedValue;
enumValueCollection.Add(values.GetValue(index));
}
index--;
}
if((enumValueCollection.Count == 0) && (currentEnumValue == 0L) && (index == 0) && (System.Convert.ToUInt64(values.GetValue(index)) == 0L))
{
enumValueCollection.Add(values.GetValue(index));
}
enumValueCollection.Reverse();
enumCompletelyCovered = (currentEnumValue == 0L) && (enumValueCollection.Count > 0);
}
enumValues = enumValueCollection;
return enumCompletelyCovered;
}
Hope this helps, -Parx
|
|
|
|
 |
|
 |
I've just written a nice console application that uses reflection to analyse any assembly for enumerated types for description attributes. It generates a minimal XML documentation set with the descriptions used as the summaries for each enumeration. This is really useful for keeping your documentation of error codes, descriptors in sync with your source code -> if you use NANT & NDoc.
It also can accept pre-generated XML doc files and insert the summaries into the correct member positions.
I will post the application on a public HTTP site soon.
Any ideas and suggestions would be
|
|
|
|
 |
|
 |
I liked this. Great idea!! Here it is going an idea on the message posted by Uwe Keim. The attribute could contain a property that would be destined to the name of resource (e.g. "AnyLib.Resources.Common.Yes"). In moment of solving string (GetDescription method) the mechanism could verify this property, if search be filled out the resource otherwise it uses the default description (hardcoded).
Marcelo Palladino
Brazil
|
|
|
|
 |
|
 |
Your way to get the description of an enum is very good but a bit too involved
what about this:
Public Enum ProfilePermission
Manager= 0
Supervisors= 1
Directors=2
End Enum
MsgBox(System.Enum.GetName(GetType(ProfilePermission), ProfilePermission.Supervisors)
Thanks A lot
Gabriel
vbnetuk@yahoo.co.uk
|
|
|
|
 |
|
 |
That's a possible solution as long as the description doesn't contain any characters that are valid for an enum. I can't use enum names like this is a very long description { ## ... }.
reto
|
|
|
|
 |
|
 |
Hi
I didnt know that
Are you saying that using the system enum is not good if you are using long description or if the enum contains characters that are in the enum>?
Thanks
Thanks A lot
Gabriel
vbnetuk@yahoo.co.uk
|
|
|
|
 |
|
 |
sorry if I wasn't clear
enum entries can only contain characters which the compiler can compile.
this means that there cannot be spaces and any other funny characters...
reto
|
|
|
|
 |
|
 |
Reto,
I think you need to embellish us with a more detailed look at enumerations with spaces. I understand what you are saying about invalid characters, but the example isn't quite clear on it. Especially if you have an XML enumeration that you would be using in a web service environment.
In a web service environment, if the service uses a string enumeration VS. compacts the names for the enumeration and lays on the XMLAttribute tag with the actual name for the value which contained spaces.
example:
///
[System.Xml.Serialization.XmlEnumAttribute("Accounts Receivable")]
AccountsReceivable,
If you then want to present the correct strings in a listbox you must first iterate through the enum and create a new string[] containing the display strings.
When the user selects an element, the string must be compressed again back to the no spaces version which matches the enum names so that the correct enum entry can be located. whew!!
Russ DeVaul
Software Pathologist
|
|
|
|
 |
|
 |
I like the above proposal using Reflection.
Previously I used Enums with _ between each part of the name
Then my code did a replace of _ with space.
And the reverse for converting a description to an enum.
eg
public enum eFoods
{
Baked_Beans,
New_Potatoes
}
...
public static string ItemColumnTitle(eFoods pColumn)
{
string lItemName = Enum.Format(typeof(eFoods), pColumn, "G");
return(lItemName.Replace("_", " "));
}
|
|
|
|
 |
|
 |
They are two different things if I understand your message correctly. Just like property name and property description are two different attributes of a property.
Enum name is more useful for developers and compilers. It is not practically useful for display to end user since it has all the naming limits and is not localizable.
Just like the other comment from other thread, it would be nice to have this attribute to use resources instead of hard coded string. Also performance would be a concern since it uses reflection alot. How about caching the result from GetDescription static method?
Just my 2 cents
Conrad:->
|
|
|
|
 |
|
|
 |
|
 |
Yes, it is. Derive from DescriptionAttribute and override the Description property. Microsoft does this throughout their code, including an SR (localized resources) class with a set of SRCategoryAttribute and SRDescriptionAttribute classes. (CategoryAttributes is localized a little differently).public class LRDescriptionAttribute : System.ComponentModel.DescriptionAttribute
{
private bool replaced;
public LRDescriptionAttribute(string description) : base(description)
{
this.replaced = false;
}
public override string Description
{
get
{
if (!this.replaced)
{
string desc = LR.GetString(base.Description);
this.DescriptionValue = desc;
this.replaced = true;
}
return base.Description;
}
}
}Just pass the name of the localized resource entry as the "description":[LRDescription("MyControl_Desc")]public class MyControl { }
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
 |
|
 |
Just tried out. Be sure to include the [AttributeUsage(AttributeTargets.All)] declaration to the definition of the LRDescriptionAttribute class.
E.g.
[AttributeUsage(AttributeTargets.All)]
public class LRDescriptionAttribute :
System.ComponentModel.DescriptionAttribute
{
...
}
--
Affordable Windows-based CMS: www.zeta-producer.com
|
|
|
|
 |