简体   繁体   English

在C#中的切换情况下分配枚举

[英]assign enum in switch case in c#

I need to change my Enum in switch case statement like below 我需要像下面这样更改case语句中的Enum

var en; 
switch (RequestTypID.ToString())
            {
                case "15":
                    en = enum1;
                    break;
                case "16":
                    en = enum2;
                    break;
                case "14":
                    en = enum3;
                    break;
                case "13":
                    en = enum4;
                    break;
                default:
                    break;
            }

and then pas it to a foreach loop like this 然后将其粘贴到这样的foreach循环中

foreach (var status in Enum.GetValues(typeof(en)))

i`m geeting an error that says en must be initialize ? 我遇到错误,提示必须进行初始化? what type should i declare for en variable ? 我应该为en变量声明什么类型? what type should i declare for status variable ? 我应该为状态变量声明什么类型?

alright i`ve added this line to the code and one problem is solved; 好吧,我已经在代码中添加了这一行,并解决了一个问题;

var en = typeof(enum1);

but now it say it cant find en reference in my for each loop ? 但是现在它说找不到每个循环中的en参考?

i also change my switch case to something like this 我也将开关盒改成这样

switch (RequestTypID.ToString())
            {
                case "15":
                    en = typeof(enum1);
                    break; 
                    ....

i`m geeting an error that says en must be initialize ? 我遇到错误,提示必须进行初始化?

This is reasonable, because this var en; 这是合理的,因为此var en; is not correct. 是不正确的。 When we want to declare something implicitly, we have to assign a value to it, when we declare it, in order the compiler infer it's type. 当我们想隐式声明某些东西时,我们必须在声明它时给它赋一个值,以便编译器推断出它的类型。

For instance, var n = 4; 例如, var n = 4; . The 4 can be stored to a variable of type int . 可以将4存储到int类型的变量中。 Hence the compiler, when see this declaration understands that the tyoe of n int. 因此,编译器在看到此声明时会理解n int的类型。

You can't create an variable with var ( var isn't a type) and not assign a value to it. 您不能使用var创建变量( var不是类型),也不能为其分配值。 Var means that compiler will evaluate it's type seeing it's value, but here you don't have a value of the variable.So you need to assign a value to your variable en .Assign a value which type is enum1, enum2, enum3, enum4 .. Var表示编译器将查看它的值来评估其type ,但是这里没有变量的值,因此需要为变量en分配一个值。分配一个类型为enum1,enum2,enum3,enum4的值 ..

You have to have another variable to the right side of the var statement so the compile know what to expect. 您必须在var语句的右侧添加另一个变量,以便编译器知道预期的结果。

You should do the following: 您应该执行以下操作:

var en = YourEnum.Option1;

or 要么

YourEnum en;

Enum.GetValues require a Type as parameter. Enum.GetValues需要类型作为参数。 So simply declare you en as Type . 所以,简单地宣布,你enType

Type en = null;

you cannot declare it as var without initializing it: compiler need to know what is the real type you want to use. 您不能在不初始化的情况下将其声明为var :编译器需要知道您要使用的实际类型是什么。

also, you have to change in your switch: 另外,您还必须更改开关:

en = typeof(SomeEnum);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM