从变量动态分配枚举选择 [英] Dynamically assign enumeration selection from variable

查看:58
本文介绍了从变量动态分配枚举选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新,使用枚举器.我会根据数据库表中设置的值来设置枚举器的值.下面是简化的 尝试显示我要执行的操作...我愿意接受任何执行此操作的方法,而不必进行任何切换.  第二个RouteMethod上也出现错误:该范围中已经定义了本地变量名称"RouteMethod".

Hi, I'm pretty new at using enumerators.  I would set the value of the enumerator based on a value set in a database table.  Below is a simplified attempt to show what I'm trying to do ... I'm open to any methods to do this, doesn't have to be a switch.  I am also getting an error on the 2nd RouteMethod saying: a local variable name 'RouteMethod' is already defined in this scope.

static string method1()

//value from database
String RM = dbConfig("RouteMethod").ToString();
            switch (RM)
            {
                case "Practical":
                    RouteType RouteMethod = RouteType.Practical;
                    break;
                case "Shortest":
                    RouteType RouteMethod = RouteType.Shortest;
                    break;
                case "National":
                    RouteType RouteMethod = RouteType.National;
                    break;
                case "FiftyThree":
                    RouteType RouteMethod = RouteType.FiftyThree;
                    break;
                case "AvoidToll":
                    RouteType RouteMethod = RouteType.AvoidToll;
                    break;
                case "Air":
                    RouteType RouteMethod = RouteType.Air;
                    break;
                default:
                    RouteType RouteMethod = RouteType.Practical;
                    break;
            }   

推荐答案

RouteType.RouteMethod = Enum.Parse(RouteType, dbConfig("RouteMethod").ToString());


这样可以简化一下.假设您的字符串与RouteType枚举匹配.


This should simplify it a bit.  Assuming your strings match the RouteType enum.


这篇关于从变量动态分配枚举选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆