tags:

views:

289

answers:

3

Okay, for whatever reason I can't seem to figure this little problem out.

I have the following enum:

public enum EFeedType
{
    TypeOne = 1,
    TypeTwo = 2
}

Now, I am going to be getting the numeric value from a database. Well, I need to cast the int value from the DB to the enum type:

EDIT: The database type is integer, so I do not need to cast from string. END EDIT

EFeedType feedType = (EFeedType) feedId;

However, when I do this, and pass in value of 2 I get the following error:

Instance validation error: '2' is not a valid value for [Namespace Goes Here].EFeedType.

Any thoughts on what I might be doing wrong or missing?

EDIT Here is the code I am using:

        //GetFeed will return an int value which is pulled from the database
        int feedId = new FeedEngine().GetFeed("FeedName");
        //Convert the ID to the Enum
        EFeedType feedType = (EFeedType) feedId;
        //Set the User Control FeedType Enum to the enum
        FeedControl.FeedType = feedType;
        //Show the user control
        FeedControl.Visible = true;

EDIT - More Info

Okay, after seeing JSkeet's response, I see that If my feedId = 1 then it will set the enum value to TypeTwo instead of TypeOne like it should. Maybe I need a Default = 0 value in my enum for this to work? But there has to be a better way, because what if my values are not in sequence.

A: 

is feedId Integer ?

Aykut
Please post this as a comment when u get enough rep.
Daniel A. White
+2  A: 

Your error won't be coming from the line you showed it on. I strongly suspect it's coming from this line:

FeedControl.FeedType = feedType;

My guess is that that property is doing some validation - and that it doesn't know about the relevant value.

EDIT: Note that if you do want to find out if a value is valid, use Enum.IsDefined. Enum.Parse will not throw an exception for an incorrect numeric value, so long as it's in the right range.

Have you really posted the exact code of the enum? Because if it doesn't explicitly specify the "= 1" and "= 2" it will autoincrement from 0, and that will be your problem.

If you could demonstrate all of this with a short but complete program it would really help. There's no need to go to the database, and no need to do anything with a user control.

Jon Skeet
Yes, the error is coming when I try to set the value of the Enum property on the control. Because it is not able to find the actual value of the enum. When I look at the watch of the feedType it shows 2 versus the actualy TypeTwo value. Which is interesting, because when I pass in 1, it sets the enum value to TypeTwo, where it should be TypeOne based on the value set in the enum.
Jason Heine
@Jason: That sounds like your enum isn't as you've shown it above then...
Jon Skeet
@John: I did post the code properly, I only changed the Enum values because the actual values can't be posted for security reasons. I am putting together a test project to see if I am messing up somewhere.
Jason Heine
@Jason Heine: If you have explicitly got the enum with a value of TypeTwo = 2, but it's showing up as TypeTwo when you've got an integer value of 1, then either you've got an out of date build or possibly multiple enums with the same name confusing you.
Jon Skeet
@John: You helped me figure out what was going on when you said that the enum might not be what it should be. I had the wrong using namespace. Thanks for your help.
Jason Heine
@Jason Heine: That suggests you've got the same type name in two different namespaces. If you can *possibly* avoid that, do so :)
Jon Skeet
@John: oh i am fixing that now... :)
Jason Heine
A: 

I just tried the following and it worked perfectly. You might want to break your code out into a little test project to see what's going on.

// enum definition
    public enum EFeedType {
        TypeOne = 1,
        TypeTwo = 2,
    }

// usage
        private void TestEnum() {
            Int32 feedId = 2;
            EFeedType stuff = (EFeedType)Enum.Parse(typeof(EFeedType), feedId.ToString());
            Response.Write(stuff.ToString());
        }
Chris Lively