How can I create an enum which contains a slash?
-
Sunday, July 26, 2009 11:08 AMI want to have an enum like this but it complains that / is invalid.public enum ImageMimeType{img/bmp, img/jpeg}or how can I have an strongly typed arrayList<string> myList = new List<string>{"img/bmp", img/jpeg"};// myList.img/bmp is not possible eitherAny way I can access this?
Answers
-
Sunday, July 26, 2009 1:24 PM
No, no cannot have a slash in the eanum value name - the name has to be conformant with the C# naming rules. The best you can do is decorate each enum value with a description attribute then map back and forth between the description and the enum value (the code below could be optimized to cache description/mime type mapping) :static class Program { public enum MimeType { Unknown, [Description("img/bmp")] ImageBmp, [Description("img/jpeg")] ImageJpeg } public static MimeType ToMimeTypeValue(this string value) { foreach (FieldInfo fi in typeof(MimeType).GetFields()) { if (!fi.IsStatic) { continue; } object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs == null || attrs.Length <= 0) { continue; } DescriptionAttribute descr = (DescriptionAttribute)attrs[0]; if (0 == string.Compare(value, descr.Description)) { return (MimeType)fi.GetValue(null); } } return MimeType.Unknown; } public static string ToMimeTypeString(this MimeType value) { foreach (FieldInfo fi in typeof(MimeType).GetFields()) { if (!fi.IsStatic || (MimeType)fi.GetValue(null) != value) { continue; } object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs == null || attrs.Length <= 0) { continue; } DescriptionAttribute descr = (DescriptionAttribute)attrs[0]; return descr.Description; } return string.Empty; } static void Main(string[] args) { string stringValue = MimeType.ImageBmp.ToMimeTypeString(); Console.WriteLine("{0} : {1}", MimeType.ImageBmp, stringValue); Console.WriteLine("{0} : {1}", stringValue, stringValue.ToMimeTypeValue()); } }HTHPaul- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM
-
Sunday, July 26, 2009 1:35 PMModerator
You can't. The "/" or the division operator is considered a mathematical symbol (Sm) as far as character classes in Unicode is concerned. According to 2.4.2 of the C# spec, mathematical symbols are not allowed for identifiers in C#. You might find something that works in OmegaMan's workaround.
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM
-
Sunday, July 26, 2009 2:30 PMModerator
The notion of a "strongly typed array" that contains strings doesn't make sense. You always need code to convert strings to enums. Enum.GetNames() can help but in this case you should just write it explicitly:
public enum ImageMimeType {
imgBmp, imgJpeg
}
public static ImageMimeType MapImageType(string type) {
switch (type.ToLower()) {
case "img/bmp": return ImageMimeType.imgBmp;
case "img/jpeg": return ImageMimeType.imgJpeg;
default: throw new ArgumentException("Invalid image type");
}
}
Hans Passant.- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM
All Replies
-
Sunday, July 26, 2009 1:24 PM
No, no cannot have a slash in the eanum value name - the name has to be conformant with the C# naming rules. The best you can do is decorate each enum value with a description attribute then map back and forth between the description and the enum value (the code below could be optimized to cache description/mime type mapping) :static class Program { public enum MimeType { Unknown, [Description("img/bmp")] ImageBmp, [Description("img/jpeg")] ImageJpeg } public static MimeType ToMimeTypeValue(this string value) { foreach (FieldInfo fi in typeof(MimeType).GetFields()) { if (!fi.IsStatic) { continue; } object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs == null || attrs.Length <= 0) { continue; } DescriptionAttribute descr = (DescriptionAttribute)attrs[0]; if (0 == string.Compare(value, descr.Description)) { return (MimeType)fi.GetValue(null); } } return MimeType.Unknown; } public static string ToMimeTypeString(this MimeType value) { foreach (FieldInfo fi in typeof(MimeType).GetFields()) { if (!fi.IsStatic || (MimeType)fi.GetValue(null) != value) { continue; } object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), false); if (attrs == null || attrs.Length <= 0) { continue; } DescriptionAttribute descr = (DescriptionAttribute)attrs[0]; return descr.Description; } return string.Empty; } static void Main(string[] args) { string stringValue = MimeType.ImageBmp.ToMimeTypeString(); Console.WriteLine("{0} : {1}", MimeType.ImageBmp, stringValue); Console.WriteLine("{0} : {1}", stringValue, stringValue.ToMimeTypeValue()); } }HTHPaul- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM
-
Sunday, July 26, 2009 1:35 PMModerator
You can't. The "/" or the division operator is considered a mathematical symbol (Sm) as far as character classes in Unicode is concerned. According to 2.4.2 of the C# spec, mathematical symbols are not allowed for identifiers in C#. You might find something that works in OmegaMan's workaround.
David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM
-
Sunday, July 26, 2009 2:30 PMModerator
The notion of a "strongly typed array" that contains strings doesn't make sense. You always need code to convert strings to enums. Enum.GetNames() can help but in this case you should just write it explicitly:
public enum ImageMimeType {
imgBmp, imgJpeg
}
public static ImageMimeType MapImageType(string type) {
switch (type.ToLower()) {
case "img/bmp": return ImageMimeType.imgBmp;
case "img/jpeg": return ImageMimeType.imgJpeg;
default: throw new ArgumentException("Invalid image type");
}
}
Hans Passant.- Marked As Answer by Dynamic Sunday, July 26, 2009 9:22 PM

