471,577 Members | 1,586 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,577 software developers and data experts.

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("Blue"); *********** Unreachable code detected
}
else
{
return ("");
}
}


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

Jan 22 '06 #1
6 27349
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
"df********@hotmail.com" wrote:
Can I do this in an if statement?
public string getClass()
{
counta = counta + 1;
if (counta < 2 )
{
MyClass mc = new MyClass();
setTheClass("Blue"); *********** Unreachable code detected
}
else
{
return ("");
}
}


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

Jan 22 '06 #2
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 "setTheClass" 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

<df********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Can I do this in an if statement?
public string getClass()
{
counta = counta + 1;
if (counta < 2 )
{
MyClass mc = new MyClass();
setTheClass("Blue"); *********** Unreachable code detected
}
else
{
return ("");
}
}


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

Jan 22 '06 #3
Trying to set a var
I get blue squig under "MyClass" says "Unreachable code detected "

public string getClass()
{
counta = counta + 1;
if (counta < 2 )
{
return ("rank_1");
MyClass mc = new MyClass();
mc.SetClass("rank_1");
}

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

}
}

Jan 22 '06 #4
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(rank);
return rank;
}
else
{
mc.SetClass("rank_6");
return String.Empty;
}
}

--
Tim Wilson
..NET Compact Framework MVP

<df********@hotmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
Trying to set a var
I get blue squig under "MyClass" says "Unreachable code detected "

public string getClass()
{
counta = counta + 1;
if (counta < 2 )
{
return ("rank_1");
MyClass mc = new MyClass();
mc.SetClass("rank_1");
}

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

}
}

Jan 22 '06 #5
df********@hotmail.com <df********@hotmail.com> wrote:
Trying to set a var
I get blue squig under "MyClass" says "Unreachable code detected "

public string getClass()
{
counta = counta + 1;
if (counta < 2 )
{
return ("rank_1");
MyClass mc = new MyClass();
mc.SetClass("rank_1");
}

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

}
}


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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 22 '06 #6
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#

Jan 22 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Gernot Frisch | last post: by
8 posts views Thread by teddysnips | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by
1 post views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.