Re: Binding a switch to an Enum at compile time?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: cody (deutronium_at_gmx.de)
Date: 01/06/05


Date: Thu, 6 Jan 2005 16:55:06 +0100


> System.Diagnostics.Debug.Assert(false, "unhandled enum value");

For such things

System.Diagnostics.Debug.Fail("unhandled enum value");

is shorter :)

"Benoit Vreuninckx" <bvnews1@nospam.skynet.be> schrieb im Newsbeitrag
news:41dd4bf3$0$2560$ba620e4c@news.skynet.be...
> Adam Blair wrote:
> > Is it possible to bind a switch statement to an Enum such that a
compile-time
> > error is raised if not all values within the Enum are handled in the
switch
> > statement? I realise you can use default: to catch unhandled cases, but
of
> > course this is only at run-time.
> >
> > Example:
> > public enum MyEnum
> > {
> > one, two, three, four
> > }
> >
> > ....
> >
> > MyEnum myVar;
> > switch (myVar)
> > {
> > case MyEnum.one:
> > break;
> > case MyEnum.two:
> > break;
> > case MyEnum.three:
> > break;
> > // I would like this to raise a compilation error as there is no
case
>
> default:
> System.Diagnostics.Debug.Assert(false, "unhandled enum value");
> break;
>
> > for MyVar.four
> > }
> >
> > Thanks, Adam
> >
>
> That compiler option does not exist (or I must be unaware of it).
> I suggest calling Debug.Assert(false) in the default clause. This way
> you are notified of any unhandled values, that is, in the debug version
> of your application. The release version will silently ignore the
> assert statement. You could also throw an exception in the default
> case, but this would also happen in the release version, which might not
> be very appealing.
> BTW, I added some code inline.
>
> Cheers,
>
> Benoit.



Relevant Pages

  • Re: Long switch statements (and Swing application structural design)
    ... In one application I have a long switch statement that looks like this ... I don't know off-hand whether you can make an `enum' abstract, ... but "You can declare method abstract in the enum type and ... home dot woh dot rr dot com slash jbmatthews ...
    (comp.lang.java.programmer)
  • Re: enums, using methods as initializers
    ... f.extract should NOT have a switch statement, ... Say someone wants to add a custom field extractor, its easy to change these enums into a regular class, and have the extract be a method in an interface. ... and I would agree that polymorphism is much more elegant. ... To me, an enum and a switch work nicely together, especially when the alternative is to write different versions of a big, complicated method in the definition of an enum. ...
    (comp.lang.java.programmer)
  • Re: Newbie: Problems Picking up integer values (dereferencing problem?) Really Trying To ReadTheGosh
    ... rather than just omitting it. ... the colon after the label must be followed by a statement ... the default label is at the very end of the switch statement. ... the enum, or a default. ...
    (comp.lang.c)
  • Binding a switch to an Enum at compile time?
    ... Is it possible to bind a switch statement to an Enum such that a compile-time ... MyEnum myVar; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Binding a switch to an Enum at compile time?
    ... "Adam Blair" wrote in message ... > enum range. ... These are also reasons why it might be best to use FxCop to detect ... desirable to omit particular values and/or a default clause, and FxCop ...
    (microsoft.public.dotnet.languages.csharp)