Clear, accurate, high quality discussion

Forum

 
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...Enums and Type safetyEnums and Type safety
Previous Previous
 
Next Next
New Post
 19/03/2008 18:08
 

I've always thought Enums were great for type safety.  Then I came across some behaviour that really disappointed me, and I'm not quite sure what the best approach should be.

I have a "gender" enumeration, and the database stores the gender as 1 or 2.  Somehow, a 3 crept in there.  I would have expected an exception when retrieving the Int32 and casting it as the Enum - but that's not what happened.  Instead, I ended up with an enum with an invalid value.  Calling it from a console app, it displayed: Gender:3, rather than Gender:Male or Gender:Female.  More sophisticated UIs broke.  So, insteady of getting a cast exception, we end up with a broken user interface.  Ugly. 

I looked for an equivalent of System.Enum.Parse which would, perhaps, force an error when the wrong integer value is used, but there does not seem to be one.  I am left thinking that I should have stored the strings "Male" and "Female" in my database, and parsed those to ensure I get the proper exception at the right part of the process.  Is that right, or is there a way to get proper enum type safety with int32s?

New Post
 20/03/2008 02:23
 

Hi

Use Enum.IsDefined to check whether the numeric value from the database is a valid enum value.  (Better still, write a generic version of this method that works for any enum type.)

private static GenderEnum ParseGenderEnum(int genderNum)
{
    if (Enum.IsDefined(typeof(GenderEnum), genderNum))
    {
        return (GenderEnum)Enum.Parse(typeof(GenderEnum), genderNum.ToString());
    }
    else
    {
        throw new ArgumentOutOfRangeException();
    }
}

Cheers

Chris

New Post
 20/03/2008 08:13
 

Hi Chris,

We can shrink that a little.  If IsDefined returns true then we know a cast is safe and legal...

private static GenderEnum ParseGenderEnum(int genderNum)
{
    if (Enum.IsDefined(typeof(GenderEnum), genderNum))
    {
        return (GenderEnum) genderNum;
    }
    else
    {
        throw new ArgumentOutOfRangeException();
    }
}

 

I didn't actually compile that code so there may be typos in there.


Cheers,
- Richard
If this post helped you over a problem, or taught you something new, please login and rate it. Ratings are in the drop down in the top left corner
New Post
 10/04/2008 02:19
 

Good point, my original version was a generic method took an object parameter, which could be either the integral or string value.  I also saw recently that MS advise against using Enum.IsDefined, and it's better to validate your enum values explicitly.

For the gender enum example in the original question, maybe you could change the underlying itegral type to bool, then your enum will be restricted to a domain two possible values?

Cheers

Chris

Previous Previous
 
Next Next
ForumForumDiscussions and...Discussions and....Net framework ....Net framework ...Enums and Type safetyEnums and Type safety

Forum Usage Guidelines

The forums are a place for all to exchange ideas and techniques, and to post and answer questions.  All are welcome to read, registration is required to post. 

If you learn somthing new, discover or acquire a new technique, then please take a moment to register and rate the post that just helped you.  This site does not send spam and it does not release your personal details.  Full details in the site privacy policy.

We have some simple posting guidelines to keep the forums a pleasant and informative environment.

  • No flames, no trolls
  • No profanity, no racism
  • Site management has the final word on approving or removing any thread, post, or comment
  • English language only please