0

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
}
10
  • 4
    Rather than using Convert.ChangeType, why don't you just provide a constructor for ScaleGroup which takes an int parameter? Commented Jan 21, 2014 at 19:08
  • @JonSkeet I didn't think about it since I was parsing Commented Jan 21, 2014 at 19:10
  • @JonSkeet How would one do that in a reader? Commented Jan 21, 2014 at 19:10
  • Well you're parsing, but then you've got an int, so anything after that point doesn't need to do any parsing... It's not clear what you mean by "in a reader" Commented Jan 21, 2014 at 19:11
  • 1
    Oh, you hadn't mentioned that it's an enum. Indeed, you claimed it's a class - but it's not. Just cast... Commented Jan 21, 2014 at 19:17

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

That did it! My colleague made this enum in simple terms what is it for?
You should ask your colleague what it's for... I have no idea what you're doing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.