Unreachable code detected

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dfetrow410@hotmail.com

    #1

    Unreachable code detected

    Can I do this in an if statement?


    public string getClass()
    {
    counta = counta + 1;


    if (counta < 2 )
    {
    MyClass mc = new MyClass();
    setTheClass("Bl ue"); *********** Unreachable code detected
    }
    else
    {
    return ("");
    }
    }




    class MyClass
    {
    private string x;
    public void SetClass(string i)
    {
    x = i;
    }
    public string GetClass()
    {
    return x;
    }
    }

  • Dale

    #2
    RE: Unreachable code detected

    What exactly are you trying to do in the if statement? Are you saying that
    you're getting a warning about unreachable code with the sample you showed?
    --
    Dale Preston
    MCAD C#
    MCSE, MCDBA


    "dfetrow410@hot mail.com" wrote:
    [color=blue]
    > Can I do this in an if statement?
    >
    >
    > public string getClass()
    > {
    > counta = counta + 1;
    >
    >
    > if (counta < 2 )
    > {
    > MyClass mc = new MyClass();
    > setTheClass("Bl ue"); *********** Unreachable code detected
    > }
    > else
    > {
    > return ("");
    > }
    > }
    >
    >
    >
    >
    > class MyClass
    > {
    > private string x;
    > public void SetClass(string i)
    > {
    > x = i;
    > }
    > public string GetClass()
    > {
    > return x;
    > }
    > }
    >
    >[/color]

    Comment

    • Tim Wilson

      #3
      Re: Unreachable code detected

      That doesn't look like unreachable code to me. Maybe I've overlooked
      something. Are you sure you don't have a return statement, or something,
      before the call to the "setTheClas s" method? It looks like you're missing a
      return in there.

      if (something)
      {
      return a;
      }
      else
      {
      return b;
      }

      .... or ...

      if (something)
      {
      return a;
      }
      return b;

      --
      Tim Wilson
      ..NET Compact Framework MVP

      <dfetrow410@hot mail.com> wrote in message
      news:1137896080 .558421.269070@ g43g2000cwa.goo glegroups.com.. .[color=blue]
      > Can I do this in an if statement?
      >
      >
      > public string getClass()
      > {
      > counta = counta + 1;
      >
      >
      > if (counta < 2 )
      > {
      > MyClass mc = new MyClass();
      > setTheClass("Bl ue"); *********** Unreachable code detected
      > }
      > else
      > {
      > return ("");
      > }
      > }
      >
      >
      >
      >
      > class MyClass
      > {
      > private string x;
      > public void SetClass(string i)
      > {
      > x = i;
      > }
      > public string GetClass()
      > {
      > return x;
      > }
      > }
      >[/color]


      Comment

      • dfetrow410@hotmail.com

        #4
        Re: Unreachable code detected

        Trying to set a var
        I get blue squig under "MyClass" says "Unreachabl e code detected "

        public string getClass()
        {
        counta = counta + 1;


        if (counta < 2 )
        {
        return ("rank_1");
        MyClass mc = new MyClass();
        mc.SetClass("ra nk_1");
        }

        else
        {
        return ("");
        MyClass mc = new MyClass();
        mc.SetClass("ra nk_6");

        }
        }

        Comment

        • Tim Wilson

          #5
          Re: Unreachable code detected

          The "returns" need to be the last line in a code block. So what you're
          attempting to do now is return before creating an instance of MyClass. Thus,
          at that point the two lines of code under the return statements will never
          be reached. Try this instead.

          public string getClass()
          {
          counta++;
          MyClass mc = new MyClass();
          if (counta < 2 )
          {
          string rank = "rank_1";
          mc.SetClass(ran k);
          return rank;
          }
          else
          {
          mc.SetClass("ra nk_6");
          return String.Empty;
          }
          }

          --
          Tim Wilson
          ..NET Compact Framework MVP

          <dfetrow410@hot mail.com> wrote in message
          news:1137898818 .063504.101870@ g43g2000cwa.goo glegroups.com.. .[color=blue]
          > Trying to set a var
          > I get blue squig under "MyClass" says "Unreachabl e code detected "
          >
          > public string getClass()
          > {
          > counta = counta + 1;
          >
          >
          > if (counta < 2 )
          > {
          > return ("rank_1");
          > MyClass mc = new MyClass();
          > mc.SetClass("ra nk_1");
          > }
          >
          > else
          > {
          > return ("");
          > MyClass mc = new MyClass();
          > mc.SetClass("ra nk_6");
          >
          > }
          > }
          >[/color]


          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Unreachable code detected

            dfetrow410@hotm ail.com <dfetrow410@hot mail.com> wrote:[color=blue]
            > Trying to set a var
            > I get blue squig under "MyClass" says "Unreachabl e code detected "
            >
            > public string getClass()
            > {
            > counta = counta + 1;
            >
            >
            > if (counta < 2 )
            > {
            > return ("rank_1");
            > MyClass mc = new MyClass();
            > mc.SetClass("ra nk_1");
            > }
            >
            > else
            > {
            > return ("");
            > MyClass mc = new MyClass();
            > mc.SetClass("ra nk_6");
            >
            > }
            > }[/color]

            Yes, now you've posted different code, we can see why - "return"
            returns immediately. In this case you can just move the return
            statements to the ends of the blocks.

            As a side note, this is an important example of why it's important to
            post real code - the same code which is producing the problem -
            especially if you don't understand exactly what's going on.

            --
            Jon Skeet - <skeet@pobox.co m>
            http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
            If replying to the group, please do not mail me too

            Comment

            • dfetrow410@hotmail.com

              #7
              Re: Unreachable code detected

              You da man Tim. Sorry about the code change. I had old code in my text
              editor

              YOU GUYS SAVE LIVES ON HERE.

              Thanks for takeing the time to help us newbies to C#

              Comment

              Working...