By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,581 Members | 1,990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,581 IT Pros & Developers. It's quick & easy.

Unreachable code detected

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.