473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Unreachable expression code detected

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)

C#

bool e = ((int)MyVariable 0) ? true : false;

It compiles and runs fine under C#, but the "false" word has a wiggly
green line underneath and the legend in the Warnings tab of the Errors
pane:

"Unreachable expression code detected"

Don't understand - can anyone shed light?

Thanks

Edward

Jun 21 '07 #1
8 4449
First - you can just use:

bool e = ((int)MyVariable 0);

Now; how is MyVariable defined? This should be fine - however, if
MyVariable is a "const", then the compiler will be evaluating this at
compile-time, so yes: the warning is correct.

Marc

Jun 21 '07 #2
You don't say what type MyVariable is, but it looks as though it casts to an
int OK.

So (int)MyVarialbe 0 is already a boolean expression. Therefore I think
you only need:

bool e = (int)MyVariable 0;

Of course if MyVariable is something that always resolves to a value greater
than zero when cast to an integer, then it will always resolve to true.

HTH
Peter

<te********@hotmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
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)

C#

bool e = ((int)MyVariable 0) ? true : false;

It compiles and runs fine under C#, but the "false" word has a wiggly
green line underneath and the legend in the Warnings tab of the Errors
pane:

"Unreachable expression code detected"

Don't understand - can anyone shed light?

Thanks

Edward

Jun 21 '07 #3
te********@hotmail.com wrote:
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)

C#

bool e = ((int)MyVariable 0) ? true : false;

It compiles and runs fine under C#, but the "false" word has a wiggly
green line underneath and the legend in the Warnings tab of the Errors
pane:

"Unreachable expression code detected"

Don't understand - can anyone shed light?
The compiler knows that ((int)MyVariable 0) is always true.

Alun Harford
Jun 21 '07 #4
On 21 Jun, 19:06, Alun Harford <devn...@alunharford.co.ukwrote:
teddysn...@hotmail.com wrote:
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)
C#
bool e = ((int)MyVariable 0) ? true : false;
It compiles and runs fine under C#, but the "false" word has a wiggly
green line underneath and the legend in the Warnings tab of the Errors
pane:
"Unreachable expression code detected"
Don't understand - can anyone shed light?

The compiler knows that ((int)MyVariable 0) is always true.
But it isn't! MyVariable can (and sometimes is) exactly 0.

Edward

Jun 22 '07 #5
On 21 Jun, 17:10, "Peter Bradley" <pbrad...@uwic.ac.ukwrote:
You don't say what type MyVariable is, but it looks as though it casts to an
int OK.

So (int)MyVarialbe 0 is already a boolean expression. Therefore I think
you only need:

bool e = (int)MyVariable 0;
Doh! That's much more elegant. Thanks.

Edward

Jun 22 '07 #6
On Jun 22, 8:38 am, teddysn...@hotmail.com wrote:
Don't understand - can anyone shed light?
The compiler knows that ((int)MyVariable 0) is always true.

But it isn't! MyVariable can (and sometimes is) exactly 0.
That sounds like it's a compiler bug then. I know you've now got a
workaround, but could you post a short but complete example of the
original problem so we can investigate further? See
http://pobox.com/~skeet/csharp/complete.html for what I mean by that.

Jon

Jun 22 '07 #7
te********@hotmail.com wrote:
On 21 Jun, 19:06, Alun Harford <devn...@alunharford.co.ukwrote:
>teddysn...@hotmail.com wrote:
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)
C#
bool e = ((int)MyVariable 0) ? true : false;
It compiles and runs fine under C#, but the "false" word has a wiggly
green line underneath and the legend in the Warnings tab of the Errors
pane:
"Unreachable expression code detected"
Don't understand - can anyone shed light?

The compiler knows that ((int)MyVariable 0) is always true.

But it isn't! MyVariable can (and sometimes is) exactly 0.

Edward
Hi Edward,

What *is* MyVariable? Please could you provide a larger coding context?

--
Tom Spink
University of Edinburgh
Jun 22 '07 #8
Ysgrifennodd te********@hotmail.com:
On 21 Jun, 17:10, "Peter Bradley" <pbrad...@uwic.ac.ukwrote:
>You don't say what type MyVariable is, but it looks as though it casts to an
int OK.

So (int)MyVarialbe 0 is already a boolean expression. Therefore I think
you only need:

bool e = (int)MyVariable 0;

Doh! That's much more elegant. Thanks.

Edward
:)

The pleasure's mine
Peter
Jun 22 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kepler | last post by:
I have a situation where I need to use a Using statement that creates some records in a database. After that completes, if it completes, I need to do some file creation. Any code I'm putting...
6
by: dfetrow410 | last post by:
Can I do this in an if statement? public string getClass() { counta = counta + 1; if (counta < 2 ) {
4
by: Josh Soref | last post by:
It seems that windows file systems can have files dated to 1617, and unfortunately if you call _findfirst/_findnext on such a directory, msvcr80d will assert. Is there some provision for...
3
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"); ...
3
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.