Re: Shorthand conditional doesn't evaluate right

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



John Bailo <jabailo@xxxxxxxxxx> wrote:

string s = "ABC";

Debug.WriteLine(

s.Length>5?"greater":"lesser"

);

The compiler reports:

Operator '>' cannot be applied to operands of type 'string' and 'int'

Isn't Length an int ?

I don't think that's your real code. Could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Here's an example showing your code working fine:

using System;

class Test
{
static void Main()
{
string s = "ABC";
Console.WriteLine (s.Length>5 ? "greater":"lesser");
}
}

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: I need help
    ... fscanfwith the "%s" option reads a white-space delimited string. ... You must check the result of the fscanf() function. ... This isn't your real code, ... If you want to return an int, ...
    (comp.lang.c)
  • Re: Why?
    ... By definition, automatic conversion of objects to String occurs, because ... converts the rest of the operands to String, ... wrapper classes, as is the case for int, or by implicitly calling the ...
    (comp.lang.java.help)
  • Shorthand conditional doesnt evaluate right
    ... The compiler reports: ... Operator '>' cannot be applied to operands of type 'string' and 'int' ... Isn't Length an int? ...
    (microsoft.public.dotnet.general)
  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)