简体   繁体   中英

Converting between Int32 and enum

I have a custom type called ScaleGroup . I am trying to parse out the data (done) then convert it to ScaleGroup for comparison. ScaleGroup is an 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

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

ScaleGroup declaration:

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:

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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