enum

Search for a string in Enum and return the Enum

匿名 (未验证) 提交于 2019-12-03 02:12:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an enumeration: public enum MyColours { Red, Green, Blue, Yellow, Fuchsia, Aqua, Orange } and I have a string: string colour = "Red"; I want to be able to return: MyColours.Red from: public MyColours GetColour(string colour) So far i have: public MyColours GetColours(string colour) { string[] colours = Enum.GetNames(typeof(MyColours)); int[] values = Enum.GetValues(typeof(MyColours)); int i; for(int i = 0; i < colours.Length; i++) { if(colour.Equals(colours[i], StringComparison.Ordinal) break; } int value = values[i]; // I know all

Do you use enum types in your WCF web services?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I've heard some people saying that enums are evil and shouldn't be used in web services because of the mismatches that could occur between the server and the client if some values are assigned, or if the enum is marked with the Flags attribute. They also said that web services exposing enums are harder to maintain but couldn't really give me viable arguments. So from your experience what are the pros and cons of using enums in a WCF web service? 回答1: The reason people recommend to avoid enums in webservices is because they create subtle

What does the [Flags] Enum Attribute mean in C#?

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: From time to time I see an enum like the following: [Flags] public enum Options { None = 0, Option1 = 1, Option2 = 2, Option3 = 4, Option4 = 8 } I don't understand what exactly the [Flags] -attribute does. Anyone have a good explanation or example they could post? 回答1: The flags attribute should be used whenever the enumerable represents a collection of flags, rather than a single value. Such collections are usually manipulated using bitwise operators, for example: myProperties.AllowedColors = MyColor.Red | MyColor.Green | MyColor.Blue; Note

Parse string to enum type

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have an enum type like this as an example: public Enum MyEnum { enum1, enum2, enum3 }; I'll read a string from config file. What I need it to parse the string to MyEnum type or null o not defined. Not sure if the following codes will work (sorry for not having access to my VS right now): // example: ParseEnum<MyEnum>("ENUM1", ref eVal); bool ParseEnum<T>(string value1, ref eVal) where T : Enum { bool bRet = false; var x = from x in Enum.GetNames(typeof(T)) where string.Equals(value1, x, StringComparison. OrdinalIgnoreCase) select x; if (x

c++ mark enum value as deprecated?

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Is it possible to mark an enum value as deprecated? e.g. enum MyEnum { firstvalue = 0 secondvalue, thirdvalue, // deprecated fourthvalue }; A second prize solution would be to ifdef a MSVC and a GCC solution. 回答1: you could do this: enum MyEnum { firstvalue = 0, secondvalue, thirdvalue, // deprecated fourthvalue }; #pragma deprecated(thirdvalue) then when ever the variable is used, the compiler will output the following: warning C4995: 'thirdvalue': name was marked as #pragma deprecated EDIT This looks a bit hacky and i dont have a GCC

Using enum in Objective-C?

匿名 (未验证) 提交于 2019-12-03 02:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is this the correct (or even a valid way) to use emums in Objective-C? i.e. The menuItem is not used but just defines a list add=1, load=2, list=3 etc. enum menuItems { add = 1 , save , load , list , removeAll , remove , quit }; int optionSelect; scanf("%d", &optionSelect); switch (optionSelect) { case add: //... break; } cheers gary 回答1: If you want to give a semantic meaning to the enumeration, you can consider to define a customized type and declare the variable "optionSelect" as variable of that type! In code... typedef enum

Undocumented NSURLErrorDomain error codes (-1001, -1003 and -1004) using StoreKit

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm writing StoreKit-related code, and I'm getting some rather troubling error codes when I try to add a purchase to the queue. So far, I've experienced error codes -1003 and -1004 and I can't find anything about those codes on the internet. Running a product request returns valid product numbers, so I don't know why calls to [[SKPaymentQueue defaultQueue] addPayment:aPayment]; would fail with this undocumented problem. The same code also works without the errors on one device but not on another. The questions I have are, as of yet,

Associating enums with strings in C#

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I know the following is not possible because it has to be an int enum GroupTypes { TheGroup = "OEM", TheOtherGroup = "CMB" } From my database I get a field with incomprehensive codes (the OEM and CMB's). I would want to make this field into an enum or something else understandable. Because the target is readability the solution should be terse. What other options do I have? 回答1: I like to use properties in a class instead of methods, since they look more enum-like. Here's a example for a Logger: public class LogCategory { private LogCategory

Lookup Java enum by string value

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Say I have an enum which is just public enum Blah { A, B, C, D } and I would like to find the enum value of a string, for example "A" which would be Blah.A . How would it be possible to do this? Is the Enum.valueOf() the method I need? If so, how would I use this? 回答1: Yes, Blah.valueOf("A") will give you Blah.A . Note that the name must be an exact match, including case: Blah.valueOf("a") and Blah.valueOf("A ") both throw an IllegalArgumentException . The static methods valueOf() and values() are created at compile time and do not appear in

Java error: class, interface, or enum expected

匿名 (未验证) 提交于 2019-12-03 02:03:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I need to know the output of this code. But it's not working. Maybe the code is wrong. I'm still learning how to use Java, and I tried fixing this for hours but still no luck. Here is the code: public class A { public A() { System.out.println ("A"); } } public class B extends A { public B() { System.out.println ("B"); } } public class C extends B { public C() { System.out.println ("C"); } } public static void main(String args[]) { A a = new A(); B b = new B(); C c = new C(); } Can anyone tell me what is wrong or missing in the code? 回答1: For