Problems with switch command

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Bering

    #1

    Problems with switch command

    I am a csharp newbie and am trying to work through some code and have gotten
    myself stuck in a loop. Any help would be appreciated.
    I have the following code in place:

    public IDcCommand GetCommand(Enum Providers provider)
    {
    switch( provider )
    {
    case EnumProviders.O DBC:
    return new OdbcCommand();
    case EnumProviders.S QLClient:
    return new SqlCommand();
    case EnumProviders.O LEDB():
    return new OleDbCommand();
    }
    }

    If I leave the code as is, I get the error "Control cannot fall through from
    one case label ('case 0:') to another". If I change each case to add a break
    after the return line, then I get an unreachable code detected error. How
    can I get this code to work? I thought a return was supposed to a value exit
    from a fall through. Any help would be most appreciated.

    Thanks,

    Steve Bering


  • Mattias Sjögren

    #2
    Re: Problems with switch command

    Steve,
    [color=blue]
    > case EnumProviders.O LEDB():[/color]

    Remove the parentheses on this line.



    Mattias

    --
    Mattias Sjögren [MVP] mattias @ mvps.org

    Please reply only to the newsgroup.

    Comment

    • Steve Bering

      #3
      Re: Problems with switch command

      Yes, I made that mistake, but that was just in the post. Any ideas of the
      switch issue?

      Thanks,

      Steve Bering

      "Mattias Sjögren" <mattias.dont.w ant.spam@mvps.o rg> wrote in message
      news:eyJxhltrDH A.3532@TK2MSFTN GP11.phx.gbl...[color=blue]
      > Steve,
      >[color=green]
      > > case EnumProviders.O LEDB():[/color]
      >
      > Remove the parentheses on this line.
      >
      >
      >
      > Mattias
      >
      > --
      > Mattias Sjögren [MVP] mattias @ mvps.org
      > http://www.msjogren.net/dotnet/
      > Please reply only to the newsgroup.[/color]


      Comment

      • Trebek

        #4
        Re: Problems with switch command

        Add break statements after your case condition and you won't 'fall thru'
        from one to the other.

        EX:

        switch (condition)
        {
        case <test-condition>:
        //do something
        break; //this is what you are missing

        case <test-condition>:
        //something
        break;

        }
        "Steve Bering" <stevebering@ho tmail.com> wrote in message
        news:usjY8WtrDH A.2340@TK2MSFTN GP12.phx.gbl...[color=blue]
        > I am a csharp newbie and am trying to work through some code and have[/color]
        gotten[color=blue]
        > myself stuck in a loop. Any help would be appreciated.
        > I have the following code in place:
        >
        > public IDcCommand GetCommand(Enum Providers provider)
        > {
        > switch( provider )
        > {
        > case EnumProviders.O DBC:
        > return new OdbcCommand();
        > case EnumProviders.S QLClient:
        > return new SqlCommand();
        > case EnumProviders.O LEDB():
        > return new OleDbCommand();
        > }
        > }
        >
        > If I leave the code as is, I get the error "Control cannot fall through[/color]
        from[color=blue]
        > one case label ('case 0:') to another". If I change each case to add a[/color]
        break[color=blue]
        > after the return line, then I get an unreachable code detected error. How
        > can I get this code to work? I thought a return was supposed to a value[/color]
        exit[color=blue]
        > from a fall through. Any help would be most appreciated.
        >
        > Thanks,
        >
        > Steve Bering
        >
        >[/color]


        Comment

        • Mattias Sjögren

          #5
          Re: Problems with switch command

          Steve,
          [color=blue]
          >Yes, I made that mistake, but that was just in the post. Any ideas of the
          >switch issue?[/color]

          I assume that "IDcCommand " should actually be "IDbCommand " then?

          It would really help if you posted your actual code, so we don't have
          to guess what's wrong based on some code snippet that might not even
          have the same problem in it.



          Mattias

          --
          Mattias Sjögren [MVP] mattias @ mvps.org

          Please reply only to the newsgroup.

          Comment

          • Trebek

            #6
            Re: Problems with switch command

            Opps , sorry just re-read your post ...

            In a similiar example from some of my code, I am repeating your condition
            but receive no error when set up like this:

            switch (formatString)

            {

            case "F":

            return String.Format(" RequestID: {0}",this.Reque stID);


            case "S":

            return String.Format(" DisplayString: {0}",this.Displ ayString);


            }


            "Trebek" <trebek@inthefo rmofaquestion.c om> wrote in message
            news:3fbbdcd0$0 $89700$a0465688 @nnrp.fuse.net. ..[color=blue]
            > Add break statements after your case condition and you won't 'fall thru'
            > from one to the other.
            >
            > EX:
            >
            > switch (condition)
            > {
            > case <test-condition>:
            > //do something
            > break; //this is what you are missing
            >
            > case <test-condition>:
            > //something
            > break;
            >
            > }
            > "Steve Bering" <stevebering@ho tmail.com> wrote in message
            > news:usjY8WtrDH A.2340@TK2MSFTN GP12.phx.gbl...[color=green]
            > > I am a csharp newbie and am trying to work through some code and have[/color]
            > gotten[color=green]
            > > myself stuck in a loop. Any help would be appreciated.
            > > I have the following code in place:
            > >
            > > public IDcCommand GetCommand(Enum Providers provider)
            > > {
            > > switch( provider )
            > > {
            > > case EnumProviders.O DBC:
            > > return new OdbcCommand();
            > > case EnumProviders.S QLClient:
            > > return new SqlCommand();
            > > case EnumProviders.O LEDB():
            > > return new OleDbCommand();
            > > }
            > > }
            > >
            > > If I leave the code as is, I get the error "Control cannot fall through[/color]
            > from[color=green]
            > > one case label ('case 0:') to another". If I change each case to add a[/color]
            > break[color=green]
            > > after the return line, then I get an unreachable code detected error.[/color][/color]
            How[color=blue][color=green]
            > > can I get this code to work? I thought a return was supposed to a value[/color]
            > exit[color=green]
            > > from a fall through. Any help would be most appreciated.
            > >
            > > Thanks,
            > >
            > > Steve Bering
            > >
            > >[/color]
            >
            >[/color]


            Comment

            • Paul Robson

              #7
              Re: Problems with switch command

              Steve Bering wrote:
              [color=blue]
              > I am a csharp newbie and am trying to work through some code and have gotten
              > myself stuck in a loop. Any help would be appreciated.
              > I have the following code in place:
              >
              > public IDcCommand GetCommand(Enum Providers provider)
              > {
              > switch( provider )
              > {
              > case EnumProviders.O DBC:
              > return new OdbcCommand();
              > case EnumProviders.S QLClient:
              > return new SqlCommand();
              > case EnumProviders.O LEDB():
              > return new OleDbCommand();
              > }
              > }
              >
              > If I leave the code as is, I get the error "Control cannot fall through from
              > one case label ('case 0:') to another". If I change each case to add a break
              > after the return line, then I get an unreachable code detected error. How
              > can I get this code to work? I thought a return was supposed to a value exit
              > from a fall through. Any help would be most appreciated.
              >
              > Thanks,
              >
              > Steve Bering
              >
              >[/color]

              IDcCommand result;

              case EnumBroviders.O DBC:
              result = new OdbcCommand();

              etc.

              It's IMO not good coding practice to exit in mid function like this.

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Problems with switch command

                Steve Bering <stevebering@ho tmail.com> wrote:[color=blue]
                > Yes, I made that mistake, but that was just in the post. Any ideas of the
                > switch issue?[/color]

                Please post the *actual* code, and the *exact* error message.

                The error message I'd *expect* you to get with code like the piece you
                posted is "Not all code paths return a value."

                --
                Jon Skeet - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jon Skeet [C# MVP]

                  #9
                  Re: Problems with switch command

                  Paul Robson <autismuk@autis muk.muralichuck s.freeserve.co. uk> wrote:[color=blue]
                  > IDcCommand result;
                  >
                  > case EnumBroviders.O DBC:
                  > result = new OdbcCommand();
                  >
                  > etc.
                  >
                  > It's IMO not good coding practice to exit in mid function like this.[/color]

                  Whereas I'd consider it actually to be more readable to return as soon
                  as you know exactly what you're going to return. Why delay it?

                  There are various cases where it *does* make sense to try to have a
                  single exit point, but I'd say this isn't one of them.

                  --
                  Jon Skeet - <skeet@pobox.co m>
                  Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                  If replying to the group, please do not mail me too

                  Comment

                  • ozbear

                    #10
                    Re: Problems with switch command

                    On Wed, 19 Nov 2003 22:13:06 +0000, Paul Robson
                    <autismuk@autis muk.muralichuck s.freeserve.co. uk> wrote:

                    <snip>[color=blue]
                    >
                    >It's IMO not good coding practice to exit in mid function like this.
                    >[/color]

                    Rubbish.

                    Single entry/single exit is a paradigm that has its place but not
                    in the "general coding practice" regimen.

                    Oz

                    Comment

                    Working...