enums

How would I convert a int to enum value at runtime when I have a string and an integer? [duplicate]

天涯浪子 提交于 2020-03-18 08:52:03
问题 This question already has answers here : Create instance of unknown Enum with string value using reflection in C# (2 answers) Closed 24 days ago . What I have is a string variable that is the name of a enum. I have the integer of the enum. How would I convert that into instance of the enum itself? Enum TestEnum { One = 1, Two = 2, Three = 3 } string str = "TestEnum"; int intValue = 2; I see many posts that require you to have an instance of the enum to get it's like this highly upvoted answer

Counting the number of flags set on an enumeration

岁酱吖の 提交于 2020-03-18 03:22:31
问题 I'm sure there must be a much better way of doing this. I'm trying to do a count operation on a Flags enum. Before I was itterating over all the possible values and counting the succesful AND operations. e.g. [Flags] public enum Skills { None = 0, Skill1 = 1, Skill2 = 2, Skill3 = 4, Skill4 = 8, Skill5 = 16, Skill6 = 32, Skill7 = 64, Skill8 = 128 } public static int Count(Skills skillsToCount) { Skills skill; for (int i = 0; i < SkillSet.AllSkills.Count; i++) { skill = SkillSet.AllSkills[i];

Counting the number of flags set on an enumeration

∥☆過路亽.° 提交于 2020-03-18 03:22:19
问题 I'm sure there must be a much better way of doing this. I'm trying to do a count operation on a Flags enum. Before I was itterating over all the possible values and counting the succesful AND operations. e.g. [Flags] public enum Skills { None = 0, Skill1 = 1, Skill2 = 2, Skill3 = 4, Skill4 = 8, Skill5 = 16, Skill6 = 32, Skill7 = 64, Skill8 = 128 } public static int Count(Skills skillsToCount) { Skills skill; for (int i = 0; i < SkillSet.AllSkills.Count; i++) { skill = SkillSet.AllSkills[i];

How do I properly document python enum elements? [duplicate]

醉酒当歌 提交于 2020-03-17 11:14:29
问题 This question already has answers here : How do I put docstrings on Enums? (2 answers) Closed 2 years ago . I understand that I can add a Python docstring to an enum type as I would any other class. But how do I add documentation to an element of that type? As far as I can see there are three possibilities: class MyEnum(Enum): """ This is my enum type. """ """ Variant 1 """ a = 0, b = 1, # variant 2 c = 2, """ variant 3 """ But none of them really work consistently. If I call print(inspect

Are enums supported by JDBC?

本小妞迷上赌 提交于 2020-03-17 09:53:46
问题 I really can't find a nice enum JDBC mapping example. Is enum actually supported by JDBC? I am working with MySQL. I have an enum column, and would like to map to some Java enum. 回答1: JDBC does not support enums. You can convert a string to an enum though, so if you have a Java enum you can do something like MyEnum enumVal = MyEnum.valueOf(rs.getString("EnumColumn")); You'll have to keep your java enum and mysql enum in sync though. MyEnum.valueOf() can throw IllegalArgumentException if there

Swift - Associated value or extension for an Enum

守給你的承諾、 提交于 2020-03-17 05:25:02
问题 General question regarding swift enum. I want to create an enum of "icon" and "associate" a value to the enum case enum Icon { case plane case arrow case logo case flag } I want to create an associated image to the enum's value. And also an associated color to the enum value So for instance if it was possible to do something like: extension Icon.plane { var image = { get { return UIImage("plane.png") } } var color = { get { return UIColor.greenColor() } } } var image = Icon.arrow.image // the

Where to put/how to handle enums in Laravel?

不羁岁月 提交于 2020-03-17 05:12:50
问题 Laravel has a <select> form helper which takes as input a dictionary. I like to keep the values for all of these in a central place. For example, I might have an enum that looks like this: $phoneTypes = [ 'CELL' => "Cellular", 'HOME' => "Home", 'WORK' => "Work", ]; Which I want to use in both my view/template, and in the database: Schema::create('customers', function (Blueprint $table) { $table->increments('id'); $table->enum('pri_phone_type',array_keys($phoneTypes)); ... }); Is there a

Swift enum associated values in Objective-C

点点圈 提交于 2020-03-16 06:49:57
问题 Is there a way to use new Swift3 enums with associated value in Objective-C? Is there a way to declare/bridge Swift3 enums with associated value in Objective-C, if I develop a library and want to give Swift3 users convenient API? 回答1: I'm afraid it's not possible, Apple has a list of Swift Type Compatibility which explicitly excludes enumerations defined in Swift without Int raw value type. Reference 回答2: This is what I did: In Swift class created the enum enum Origin { case Search(searchTerm

Is there a simple way to mutate an enum field in Rust?

白昼怎懂夜的黑 提交于 2020-03-16 06:05:04
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match

Is there a simple way to mutate an enum field in Rust?

若如初见. 提交于 2020-03-16 06:02:13
问题 Suppose we have an enum that looks like this: enum MyEnum { Field1, Field2 {x: f64, y: f64}, /* Maybe some other fields */ MyString(String), } Now I created an instance of this enum of the subtype MyString and after some actions I want to mutate it. For example: fn main() { let mut my_enum = MyEnum::MyString("Hello, world".to_string()); /* Some actions */ // Mutating the string match my_enum { MyEnum::MyString(ref mut content) => { content.push('!'); }, _ => {} } // Printing the string match