1

I'm new to enum.

I've got this enum:

public enum Categories
{
    Animals,
    Animations,
    Accessories,
    Apearance,
    Clothing,
    Gadgets,
    Land,
    Scripts,
    Vehicles,
    Weapons,
    Other
}

Then I have this variable: private Categories Category; I'm trying to parse user input (string) so that Category will be equal to the right enum.

this.Category = Enum.Parse(Categories ,cat);

And I get this Error:

'Product.Categories' is a 'type' but is used like a 'variable'

I hope you understand what I'm trying to say.

2 Answers 2

4

To get the Type object to use with a method like Enum.Parse(), use the typeof operator with the type name. You also need to perform a cast from object (which it returns) to your enum:

this.Category = (Categories) Enum.Parse(typeof(Categories), cat);
Sign up to request clarification or add additional context in comments.

1 Comment

To add to the above answer, there is also an example for this on msdn
3

Pass typeof(Categories) instead of Categories and add a cast, like this:

this.Category = (Categories)Enum.Parse(typeof(Categories), cat);

This assumes that this.Category is of type Categories and is required because Enum.Parse returns a value of type, object.

1 Comment

this.Category is indeed declared as such :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.