473,667 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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;
}
}

Jan 22 '06 #1
6 27529
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********@hot mail.com" wrote:
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;
}
}

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 "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

<df********@hot mail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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;
}
}

Jan 22 '06 #3
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");

}
}

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(ran k);
return rank;
}
else
{
mc.SetClass("ra nk_6");
return String.Empty;
}
}

--
Tim Wilson
..NET Compact Framework MVP

<df********@hot mail.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
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");

}
}

Jan 22 '06 #5
df********@hotm ail.com <df********@hot mail.com> wrote:
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");

}
}


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.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2847
by: SteadySteps | last post by:
Hi I migrated a project which compiles correctly on VC 6.0 to VS 2002. However now all I get several warning that all the statements within catch blocks are "unreachable code". How can I correct this ? C++ exceptions are enabled ( /EHsc ). Thanks. SS
2
1985
by: Hovik Melikyan | last post by:
This code produces a 'unreachable code' warning at line 16 (throw new X ...) with no visible reason: #include <string> class X { std::string msg;
4
6973
by: Gernot Frisch | last post by:
I get informed that parts of my code cannot be reached. Did I do something wrong? try { if (login(progstring) > 0) { return 1; // OK } }
3
3293
by: Don Burden | last post by:
We've started converting some applications to the .NET 2.0 framework. When compiling in VS 2005, I'm getting a warning on this line: return (unitWidth != null ) ? unitWidth : new Unit("0px"); Looks valid to me, but this gives a warning with "new" underlined saying "warning CS0429: Unreachable expression code detected".
8
4465
by: teddysnips | last post by:
I'm new to C# - recent background mainly ASP.NET with VB.NET. Anyhoot, I needed to create a C# statement analogous to VB's IIf: VB.NET Dim e As Boolean e = IIf((CInt(MyVariable) 0), True, False)
3
7634
by: cie | last post by:
Hi, anyone please help me when I try to close a connection to the database from a static method, "Unreachable code detected" error occured. What's the problem actually? Thank you before
1
1722
by: ITC | last post by:
When i write web method in C#. I got a warning and can't get the right result though it run. Code is below. public string CurrencyConverter(double amt, string fromCurrency, string toCurrency) { string currencyLabel ={ "USD", "AUD", "PDS", "MYR", "SGD" };
2
2539
by: selasebytes | last post by:
how do i get over this warning to get my program running correctly?? Below is the faulty part of the code. Am trying to calculate discount on a purchase form. the codes for my discount is nested in an if statement with a switch case involved. how ever upon executing, my form dosent calculate the discount due to the warning that says "unreachable code detected". this code i guess is the calculation for my discount with the case declaration....
5
2518
by: karthik gowda | last post by:
String position = cboPositions.Text; switch (position) { case "Absolute": x = 1; y = 1; break; case "TopLeft": x = 0; y = 0; break; case "TopRight":
0
8457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8788
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.