PC Review


Reply
Thread Tools Rate Thread

Binding a switch to an Enum at compile time?

 
 
=?Utf-8?B?QWRhbSBCbGFpcg==?=
Guest
Posts: n/a
 
      6th Jan 2005
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
for MyVar.four
}

Thanks, Adam

 
Reply With Quote
 
 
 
 
Benoit Vreuninckx
Guest
Posts: n/a
 
      6th Jan 2005
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.
 
Reply With Quote
 
=?Utf-8?B?QWRhbSBCbGFpcg==?=
Guest
Posts: n/a
 
      6th Jan 2005
Thanks for your response. Your idea is useful, but unfortunately it still
relies on traversing all code paths (and developers to be watching the Output
window).

Adam

"Benoit Vreuninckx" wrote:

> 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.
>

 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      6th Jan 2005
A compilation error doesn't make sense there since during compile time, the
value of myVar is not populated.
You do want a runtime error though, and Debug.Assert is a good suggestion
there. Debug.Assert will not require the developers to look at the output
window.

You could always throw an exception for a little bit more intrusive alert.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



"Adam Blair" <Adam (E-Mail Removed)> wrote in message
news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
> 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
> for MyVar.four
> }
>
> Thanks, Adam
>



 
Reply With Quote
 
=?Utf-8?B?QWRhbSBCbGFpcg==?=
Guest
Posts: n/a
 
      6th Jan 2005
What i was after is an indication - at compilation - if there *could ever be*
a value of myVar that is not handled.

"Sahil Malik" wrote:

> A compilation error doesn't make sense there since during compile time, the
> value of myVar is not populated.
> You do want a runtime error though, and Debug.Assert is a good suggestion
> there. Debug.Assert will not require the developers to look at the output
> window.
>
> You could always throw an exception for a little bit more intrusive alert.
>
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
>
>
> "Adam Blair" <Adam (E-Mail Removed)> wrote in message
> news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
> > 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
> > for MyVar.four
> > }
> >
> > Thanks, Adam
> >

>
>
>

 
Reply With Quote
 
Sahil Malik
Guest
Posts: n/a
 
      6th Jan 2005
Right, and as I said .. the values are not populated at compile time, so the
compiler has no way of knowing what you might be sending down that code
path. Which is why the default keyword.

In short - not possible to do.

- Sahil Malik
http://dotnetjunkies.com/weblog/sahilmalik



"Adam Blair" <(E-Mail Removed)> wrote in message
news:A22F0F3A-1354-496E-8FF9-(E-Mail Removed)...
> What i was after is an indication - at compilation - if there *could ever

be*
> a value of myVar that is not handled.
>
> "Sahil Malik" wrote:
>
> > A compilation error doesn't make sense there since during compile time,

the
> > value of myVar is not populated.
> > You do want a runtime error though, and Debug.Assert is a good

suggestion
> > there. Debug.Assert will not require the developers to look at the

output
> > window.
> >
> > You could always throw an exception for a little bit more intrusive

alert.
> >
> > - Sahil Malik
> > http://dotnetjunkies.com/weblog/sahilmalik
> >
> >
> >
> > "Adam Blair" <Adam (E-Mail Removed)> wrote in message
> > news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
> > > 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
> > > for MyVar.four
> > > }
> > >
> > > Thanks, Adam
> > >

> >
> >
> >



 
Reply With Quote
 
=?Utf-8?B?QWRhbSBCbGFpcg==?=
Guest
Posts: n/a
 
      6th Jan 2005
My point is that the value myVar *must* be one of the 4 values in the Enum
MyEnum (so the compiler does in fact know what we might be sending down that
code path - it is either one, two, three or four; nothing else) but only 3 of
them are handled by the switch statement. I know that at compilation myVar
has no value, but it is logically possible to determine that the switch
statement does not handle all possible values.

regards, Adam

"Sahil Malik" wrote:

> Right, and as I said .. the values are not populated at compile time, so the
> compiler has no way of knowing what you might be sending down that code
> path. Which is why the default keyword.
>
> In short - not possible to do.
>
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
>
>
> "Adam Blair" <(E-Mail Removed)> wrote in message
> news:A22F0F3A-1354-496E-8FF9-(E-Mail Removed)...
> > What i was after is an indication - at compilation - if there *could ever

> be*
> > a value of myVar that is not handled.
> >
> > "Sahil Malik" wrote:
> >
> > > A compilation error doesn't make sense there since during compile time,

> the
> > > value of myVar is not populated.
> > > You do want a runtime error though, and Debug.Assert is a good

> suggestion
> > > there. Debug.Assert will not require the developers to look at the

> output
> > > window.
> > >
> > > You could always throw an exception for a little bit more intrusive

> alert.
> > >
> > > - Sahil Malik
> > > http://dotnetjunkies.com/weblog/sahilmalik
> > >
> > >
> > >
> > > "Adam Blair" <Adam (E-Mail Removed)> wrote in message
> > > news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
> > > > 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
> > > > for MyVar.four
> > > > }
> > > >
> > > > Thanks, Adam
> > > >
> > >
> > >
> > >

>
>
>

 
Reply With Quote
 
Nicole Calinoiu
Guest
Posts: n/a
 
      6th Jan 2005
"Adam Blair" <(E-Mail Removed)> wrote in message
news:CAB4F4C8-D344-4628-B4B8-(E-Mail Removed)...
> My point is that the value myVar *must* be one of the 4 values in the Enum
> MyEnum (so the compiler does in fact know what we might be sending down
> that
> code path - it is either one, two, three or four; nothing else)


Unfortunately, that's not the case. myVar can be assigned any value from
the enum's underlying type (usually an integer).


> but only 3 of
> them are handled by the switch statement. I know that at compilation myVar
> has no value, but it is logically possible to determine that the switch
> statement does not handle all possible values.


Your best approach here might be a custom FxCop rule. In addition to
screening for all values from the enum, you might also want to consider
also enforcing using of a default clause to handle possible values that are
not defined by the enum.


> regards, Adam
>
> "Sahil Malik" wrote:
>
>> Right, and as I said .. the values are not populated at compile time, so
>> the
>> compiler has no way of knowing what you might be sending down that code
>> path. Which is why the default keyword.
>>
>> In short - not possible to do.
>>
>> - Sahil Malik
>> http://dotnetjunkies.com/weblog/sahilmalik
>>
>>
>>
>> "Adam Blair" <(E-Mail Removed)> wrote in message
>> news:A22F0F3A-1354-496E-8FF9-(E-Mail Removed)...
>> > What i was after is an indication - at compilation - if there *could
>> > ever

>> be*
>> > a value of myVar that is not handled.
>> >
>> > "Sahil Malik" wrote:
>> >
>> > > A compilation error doesn't make sense there since during compile
>> > > time,

>> the
>> > > value of myVar is not populated.
>> > > You do want a runtime error though, and Debug.Assert is a good

>> suggestion
>> > > there. Debug.Assert will not require the developers to look at the

>> output
>> > > window.
>> > >
>> > > You could always throw an exception for a little bit more intrusive

>> alert.
>> > >
>> > > - Sahil Malik
>> > > http://dotnetjunkies.com/weblog/sahilmalik
>> > >
>> > >
>> > >
>> > > "Adam Blair" <Adam (E-Mail Removed)> wrote in message
>> > > news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
>> > > > 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
>> > > > for MyVar.four
>> > > > }
>> > > >
>> > > > Thanks, Adam
>> > > >
>> > >
>> > >
>> > >

>>
>>
>>



 
Reply With Quote
 
cody
Guest
Posts: n/a
 
      6th Jan 2005
> System.Diagnostics.Debug.Assert(false, "unhandled enum value");

For such things

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

is shorter

"Benoit Vreuninckx" <(E-Mail Removed)> schrieb im Newsbeitrag
news:41dd4bf3$0$2560$(E-Mail Removed)...
> 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.



 
Reply With Quote
 
Benoit Vreuninckx
Guest
Posts: n/a
 
      6th Jan 2005
Sahil Malik wrote:
> Right, and as I said .. the values are not populated at compile time, so the
> compiler has no way of knowing what you might be sending down that code
> path. Which is why the default keyword.
>
> In short - not possible to do.
>
> - Sahil Malik
> http://dotnetjunkies.com/weblog/sahilmalik
>
>
>
> "Adam Blair" <(E-Mail Removed)> wrote in message
> news:A22F0F3A-1354-496E-8FF9-(E-Mail Removed)...
>
>>What i was after is an indication - at compilation - if there *could ever

>
> be*
>
>>a value of myVar that is not handled.
>>
>>"Sahil Malik" wrote:
>>
>>
>>>A compilation error doesn't make sense there since during compile time,

>
> the
>
>>>value of myVar is not populated.
>>>You do want a runtime error though, and Debug.Assert is a good

>
> suggestion
>
>>>there. Debug.Assert will not require the developers to look at the

>
> output
>
>>>window.
>>>
>>>You could always throw an exception for a little bit more intrusive

>
> alert.
>
>>>- Sahil Malik
>>>http://dotnetjunkies.com/weblog/sahilmalik
>>>
>>>
>>>
>>>"Adam Blair" <Adam (E-Mail Removed)> wrote in message
>>>news:4A5AC66A-2913-4156-9C03-(E-Mail Removed)...
>>>
>>>>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
>
>>>>for MyVar.four
>>>>}
>>>>
>>>>Thanks, Adam
>>>>
>>>
>>>
>>>

>
>


Hi,

He just wanted to know if the compiler is able to check if all the
possible values of an enumeration have a corresponding case in the
switch statement. So if one adds a new value to the enumeration, the
compiler should throw a warning/error on all switch statements not
explicitly handling that new value.

Cheers,
Benoit.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
WinForms Binding to Enum Properties jehugaleahsa@gmail.com Microsoft C# .NET 1 16th Sep 2009 12:33 AM
Binding names of ENUM constants to a Combobox =?Utf-8?B?UmljaGFyZCBUb2NjaQ==?= Microsoft Dot NET Framework Forms 2 19th Jun 2007 07:51 PM
Binding a CheckBoxList to an Enum =?Utf-8?B?aGVjc2FuMDc=?= Microsoft ASP .NET 0 11th Jul 2005 10:39 PM
enum and switch salvatore.difazio@gmail.com Microsoft C# .NET 5 6th Jul 2005 02:57 PM
DataRelations in typed datasets, runtime/compile time binding ggabe Microsoft ADO .NET 2 2nd Jul 2003 04:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:55 AM.