Posts

Showing posts with the label Enums

Convert List To IEnumerable

Answer : Maybe try this? ( untested ) ViewBag.AvaiableEnums = dynamicTextEnumsAvaiable.Select(x => new SelectListItem() { Text = x.ToString() }); You could do the following ViewBag.AvaiableEnums = new SelectList(dynamicTextEnumsAvaiable) See http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist(v=vs.118).aspx You can maybe use a Linq statement to convert it IEnumerable<SelectListItem> myCollection = dynamicTextEnumsAvaiable .Select(i => new SelectListItem() { Text = i.ToString(), Value = i });

Convert String To Enum In Python

Answer : This functionality is already built in to Enum [1]: >>> from enum import Enum >>> class Build(Enum): ... debug = 200 ... build = 400 ... >>> Build['debug'] <Build.debug: 200> [1] Official docs: Enum programmatic access Another alternative (especially useful if your strings don't map 1-1 to your enum cases) is to add a staticmethod to your Enum , e.g.: class QuestionType(enum.Enum): MULTI_SELECT = "multi" SINGLE_SELECT = "single" @staticmethod def from_str(label): if label in ('single', 'singleSelect'): return QuestionType.SINGLE_SELECT elif label in ('multi', 'multiSelect'): return QuestionType.MULTI_SELECT else: raise NotImplementedError Then you can do question_type = QuestionType.from_str('singleSelect') def custom_enum(typename, items_dict): class_definition = """ from...