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