Yes no enums and goto's



  • I love working with Yes No enums...then tie that in with goto's and life is good.  Did I mention this is from a top 30 internet site.  I guess bool's just wouldn't work.

     <FONT color=#0000ff size=2>switch</FONT><FONT size=2>(searchResultsAvailable)
    {
    </FONT><FONT color=#0000ff size=2>    case</FONT><FONT size=2> SearchResultsAvailable.yes:
    </FONT><FONT size=2>       title = "Featured Products";
    </FONT><FONT color=#0000ff size=2>       break</FONT><FONT size=2>;
        </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> SearchResultsAvailable.no:
           title = "Top Sellers";
           </FONT><FONT color=#0000ff size=2>break</FONT><FONT size=2>;
        </FONT><FONT color=#0000ff size=2>default</FONT><FONT size=2>:
           </FONT><FONT color=#0000ff size=2>goto</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> SearchResultsAvailable.no;
    }

    </FONT>

     



  • @dumbphux said:

    I love working with Yes No enums...then tie that in with goto's and life is good.  Did I mention this is from a top 30 internet site.  I guess bool's just wouldn't work.

     <FONT color=#0000ff size=2>switch</FONT><FONT size=2>(searchResultsAvailable)
    {
    </FONT><FONT color=#0000ff size=2>    case</FONT><FONT size=2> SearchResultsAvailable.yes:
    </FONT><FONT size=2>       title = "Featured Products";
    </FONT><FONT color=#0000ff size=2>       break</FONT><FONT size=2>;
        </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> SearchResultsAvailable.no:
           title = "Top Sellers";
           </FONT><FONT color=#0000ff size=2>break</FONT><FONT size=2>;
        </FONT><FONT color=#0000ff size=2>default</FONT><FONT size=2>:
           </FONT><FONT color=#0000ff size=2>goto</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>case</FONT><FONT size=2> SearchResultsAvailable.no;
    }</FONT>

    Hmmm, that worked well....

    For when:

    switch (theEnum) {
      case theEnum.yes: { ...; break; }
      case theEnum.no:
      case default: { 
           ...; 
           break; 
      }
    }
    

    ... simply won't do!



  • @snoofle said:

    @dumbphux said:

    I love working with Yes No enums...then tie that in with goto's and life is good.  Did I mention this is from a top 30 internet site.  I guess bool's just wouldn't work.

     <font color="#0000ff" size="2">switch</font><font size="2">(searchResultsAvailable)
    {
    </font><font color="#0000ff" size="2">    case</font><font size="2"> SearchResultsAvailable.yes:
    </font><font size="2">       title = "Featured Products";
    </font><font color="#0000ff" size="2">       break</font><font size="2">;
        </font><font color="#0000ff" size="2">case</font><font size="2"> SearchResultsAvailable.no:
           title = "Top Sellers";
           </font><font color="#0000ff" size="2">break</font><font size="2">;
        </font><font color="#0000ff" size="2">default</font><font size="2">:
           </font><font color="#0000ff" size="2">goto</font><font size="2"> </font><font color="#0000ff" size="2">case</font><font size="2"> SearchResultsAvailable.no;
    }</font>

    Hmmm, that worked well....

    For when:

    switch (theEnum) {
    case theEnum.yes: { ...; break; }
    case theEnum.no:
    case default: {
    ...;
    break;
    }
    }

    ... simply won't do!

    Assuming this is C#, that syntax would cause a compile error. 



  • @Kyanar said:

    Assuming this is C#, that syntax would cause a compile error. 


    Why? Is C# the real WTF here? :P



  • @fennec said:

    @Kyanar said:

    Assuming this is C#, that syntax would cause a compile error. 

    Why? Is C# the real WTF here? :P

     switch (theEnum) {
      case theEnum.yes: { ...; break; }
      case theEnum.no:
      case default: {
           ...;
           break;
      }
    }

    the brackets shouldn't be there. It should actually be:
    switch (theEnum) {
      case theEnum.yes:  ...; break; 
      case theEnum.no:
      case default:  
           ...;
           break;
      }
    }



  • I hate to state the obvious but... 

    if <font size="2">(searchResultsAvailable == </font><font size="2">SearchResultsAvailable.yes)
    </font><font size="2">      title = "Featured Products";
    else
    </font><font color="#0000ff" size="2"></font><font size="2">       title = "Top Sellers";
    </font>

    ... or just use the ternary operator.

    The only reason I can see for that other code not working in C# is "case default" instead of just "default".

     

     



  • Doh, you are right...it is the case default that would blow it up. 



  • @Kyanar said:

    @snoofle said:
    @dumbphux said:

    I love working with Yes No enums...then tie that in with goto's and life is good.  Did I mention this is from a top 30 internet site.  I guess bool's just wouldn't work.

     <FONT color="#0000ff" size="2">switch</FONT><FONT size="2">(searchResultsAvailable)
    {
    </FONT><FONT color="#0000ff" size="2">    case</FONT><FONT size="2"> SearchResultsAvailable.yes:
    </FONT><FONT size="2">       title = "Featured Products";
    </FONT><FONT color="#0000ff" size="2">       break</FONT><FONT size="2">;
        </FONT><FONT color="#0000ff" size="2">case</FONT><FONT size="2"> SearchResultsAvailable.no:
           title = "Top Sellers";
           </FONT><FONT color="#0000ff" size="2">break</FONT><FONT size="2">;
        </FONT><FONT color="#0000ff" size="2">default</FONT><FONT size="2">:
           </FONT><FONT color="#0000ff" size="2">goto</FONT><FONT size="2"></FONT><FONT color="#0000ff" size="2">case</FONT><FONT size="2"> SearchResultsAvailable.no;
    }</FONT>

    Hmmm, that worked well....

    For when:

    switch (theEnum) {
    case theEnum.yes: { ...; break; }
    case theEnum.no:
    case default: {
    ...;
    break;
    }
    }

    ... simply won't do!

    Assuming this is C#, that syntax would cause a compile error. 

    Actually, C# should accept that IIRC. You can have case X: case Y: code. It only requires a jump if there's code in between.

    I don't know if that's true with case X: default: - but that's a pointless construct anyway, when you can just say default: . And a switch with 1 case and a default should just be an if/else.



  • @Kyanar said:

    @snoofle said:
    @dumbphux said:

    I love working with Yes No enums...then tie that in with goto's and life is good.  Did I mention this is from a top 30 internet site.  I guess bool's just wouldn't work.

     <font color="#0000ff" size="2">switch</font><font size="2">(searchResultsAvailable)
    {
    </font><font color="#0000ff" size="2">    case</font><font size="2"> SearchResultsAvailable.yes:
    </font><font size="2">       title = "Featured Products";
    </font><font color="#0000ff" size="2">       break</font><font size="2">;
        </font><font color="#0000ff" size="2">case</font><font size="2"> SearchResultsAvailable.no:
           title = "Top Sellers";
           </font><font color="#0000ff" size="2">break</font><font size="2">;
        </font><font color="#0000ff" size="2">default</font><font size="2">:
           </font><font color="#0000ff" size="2">goto</font><font size="2"> </font><font color="#0000ff" size="2">case</font><font size="2"> SearchResultsAvailable.no;
    }</font>

    Hmmm, that worked well....

    For when:

    switch (theEnum) {
    case theEnum.yes: { ...; break; }
    case theEnum.no:
    case default: {
    ...;
    break;
    }
    }

    ... simply won't do!

    Assuming this is C#, that syntax would cause a compile error. 

     switch (theEnum)
    {

        case theEnum.yes: { stuff(); break;}
        case theEnum.no:
        default: {    stuff(); break; } 

    }

    is perfectly fine in C#.NET 1.1. Curly braces work just fine around blocks. No and Default can point to the same block. You just can't have any sort of fall-through.
     



  • @Benanov said:

     switch (theEnum)
    {

        case theEnum.yes: { stuff(); break;}
        case theEnum.no:
        default: {    stuff(); break; } 

    }

    is perfectly fine in C#.NET 1.1. Curly braces work just fine around blocks. No and Default can point to the same block. You just can't have any sort of fall-through.
     

    Indeed.  I was confusing it with another type of fall-through construct ("case something: case somethingelse:") which did NOT work in an earlier version of the framework.  Trying it in .NET 2.0 (pleasantly) compiles as expected.


Log in to reply