Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

Unreachable expression code detected

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
teddysnips@hotmail.com - 21 Jun 2007 17:01 GMT
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
Marc Gravell - 21 Jun 2007 17:04 GMT
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
Peter Bradley - 21 Jun 2007 17:10 GMT
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

> I'm new to C# - recent background mainly ASP.NET with VB.NET.
>
[quoted text clipped - 21 lines]
>
> Edward
teddysnips@hotmail.com - 22 Jun 2007 08:39 GMT
> You don't say what type MyVariable is, but it looks as though it casts to an
> int OK.
[quoted text clipped - 3 lines]
>
> bool e = (int)MyVariable > 0;

Doh!  That's much more elegant.  Thanks.

Edward
Peter Bradley - 22 Jun 2007 19:41 GMT
Ysgrifennodd teddysnips@hotmail.com:
>> You don't say what type MyVariable is, but it looks as though it casts to an
>> int OK.
[quoted text clipped - 7 lines]
>
> Edward

:)

The pleasure's mine

Peter
Alun Harford - 21 Jun 2007 19:06 GMT
> I'm new to C# - recent background mainly ASP.NET with VB.NET.
>
[quoted text clipped - 17 lines]
>
> Don't understand - can anyone shed light?

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

Alun Harford
teddysnips@hotmail.com - 22 Jun 2007 08:38 GMT
> teddysn...@hotmail.com wrote:
> > I'm new to C# - recent background mainly ASP.NET with VB.NET.
[quoted text clipped - 20 lines]
>
> The compiler knows that ((int)MyVariable > 0) is always true.

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

Edward
Jon Skeet [C# MVP] - 22 Jun 2007 08:54 GMT
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
Tom Spink - 22 Jun 2007 09:43 GMT
>> teddysn...@hotmail.com wrote:
>> > I'm new to C# - recent background mainly ASP.NET with VB.NET.
[quoted text clipped - 24 lines]
>
> Edward

Hi Edward,

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

Signature

Tom Spink
University of Edinburgh


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.