Re: Binding a switch to an Enum at compile time?
From: cody (deutronium_at_gmx.de)
Date: 01/06/05
- Next message: Benoit Vreuninckx: "Re: Binding a switch to an Enum at compile time?"
- Previous message: Nicole Calinoiu: "Re: Binding a switch to an Enum at compile time?"
- In reply to: Benoit Vreuninckx: "Re: Binding a switch to an Enum at compile time?"
- Next in thread: Sahil Malik: "Re: Binding a switch to an Enum at compile time?"
- Messages sorted by: [ date ] [ thread ]
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.
- Next message: Benoit Vreuninckx: "Re: Binding a switch to an Enum at compile time?"
- Previous message: Nicole Calinoiu: "Re: Binding a switch to an Enum at compile time?"
- In reply to: Benoit Vreuninckx: "Re: Binding a switch to an Enum at compile time?"
- Next in thread: Sahil Malik: "Re: Binding a switch to an Enum at compile time?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|