Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
The Benefits of Assigning a Non-default Value to Enums

As part of our current application, we are often using enums for the various benefits they bring.  One problem I ran into, though, is not assigning a non-zero (for integer enums) default value to the enum.

 

Why would this be a problem?  Well, when I want to use Debug.Assert to check the preconditions on my method!  For example, I have a property whose value is an enum and the precondition is that this property has been set (a value assigned to it).  If the default value of the enum is zero, any time I check to see if the property has been assigned to, it always passes since integers without values assigned to them default to zero.  And zero is the default value of your first constant unless you override it.

 

For my purposes, I have found it useful to start numbering the values of an integer enum at one instead of zero.  That way I can write (in VB):

 

Debug.Assert((Me.PricingSize = PricingSizeEnum.FirstChoice) Or (Me.PricingSize = PricingSizeEnum.SecondChoice) Or (Me.PricingSize = PricingSizeEnum.ThirdChoice), "PricingSize must be defined before calling this method")

 

This also works (which is more intuitive but may not be exactly correct, can anyone help explain if this is ok?): 

 

Debug.Assert(Me.PricingSize <> Nothing, "PricingSize must be defined before calling this method")


Posted 11-25-2003 9:04 AM by Darrell Norton

[Advertisement]

Comments

Mark Bonafe wrote re: The Benefits of Assigning a Non-default Value to Enums
on 11-25-2003 8:41 AM
Hey Darrell. I don't know if this would be suitable for your particular situation, but you could assign the default value (0) of the enum as "UnAssigned." Of course, that would mean zero is not usable.

Using the second Debug.Assert example is probably not the best choice because invalid choices would still pass.

If PricingSize is part of an object, you could do this. Start your enum as UnAssigned = -1, make PricingSize an instance of the enum instead of an integer, and set the default value of PricingSize to enum.UnAssigned. That way, since PricingSize is an enum instead of an integer, your statement would be: Debug.Assert(object.PricingSize <> enum.UnAssigned, "PricingSize must be defined before calling this method")
Darrell wrote re: The Benefits of Assigning a Non-default Value to Enums
on 11-25-2003 10:09 AM
Interesting Bonafe. Thanks!
Udi Dahan wrote re: The Benefits of Assigning a Non-default Value to Enums
on 12-16-2003 7:45 PM
I use the above suggested "unassigned" almost any time I've got an enum, and I find it to be quite intuitive. Obviously, making a system-wide const for unassigned for all enums is necessary.