简体   繁体   English

在Int32和枚举之间转换

[英]Converting between Int32 and enum

I have a custom type called ScaleGroup . 我有一个自定义类型,称为ScaleGroup I am trying to parse out the data (done) then convert it to ScaleGroup for comparison. 我试图解析出数据(完成),然后将其转换为ScaleGroup进行比较。 ScaleGroup is an enum . ScaleGroup是一个enum I found this method online of conversion but it not working. 我在网上找到了这种转换方法,但是它不起作用。 How can I get the conversion? 我如何获得转换?

Here is my type declaration 这是我的类型声明

public ScaleGroup ScaleGroup { get; set; }

Here is where I need it to change from an Int32 to ScaleGroup 这是我需要将其从Int32更改为ScaleGroup

int num = Convert.ToInt32(ld.ScaleGroup);
int secondDigit = num % 10;
ld.ScaleGroup = (ScaleGroup)Convert.ChangeType(
       secondDigit, typeof(ScaleGroup));//problem spot

ScaleGroup declaration: ScaleGroup声明:

public enum ScaleGroup
{
    GROUP_1 = 1,
    GROUP_2 = 2,
    BOTH = 3
}

Now that we know that ScaleGroup isn't a class, but an enum, it's simple: 现在我们知道ScaleGroup 不是一个类,而是一个枚举,这很简单:

int num = (int) ld.ScaleGroup;
int secondDigit = num % 10;
ld.ScaleGroup = (ScaleGroup) secondDigit;

(It's not clear to me that that's actually what you want, given your enum declaration, but that will perform the relevant conversions...) (根据您的枚举声明,我不清楚这实际上就是您想要的,但是它将执行相关的转换...)

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

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