c# conditional operator error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul

    #1

    c# conditional operator error

    Can you help me with this error?

    string fields = string.Empty;
    ....
    fields.Length 0 ? fields += ", Name" : fields += "Name";

    Error:
    Only assignment, call, increment, decrement, and new object expressions can be used as a statement

  • Brad Wery

    #2
    Re: c# conditional operator error

    Your code should look like this:

    fields += fields.Length 0 ? ", Name" : "Name";


    Brad

    Paul wrote:
    Can you help me with this error?
    >
    string fields = string.Empty;
    ...
    fields.Length 0 ? fields += ", Name" : fields += "Name";
    >
    Error:
    Only assignment, call, increment, decrement, and new object expressions
    can be used as a statement
    >

    Comment

    • =?Utf-8?B?WWFyb24gS2Fybmk=?=

      #3
      RE: c# conditional operator error

      It's only used for assignment, not for conditional, its behave like the
      "return of "value" in case of",
      So add the conditional statement at the left site of the phrase as brad
      present.

      --
      Sincerely
      Yaron Karni
      All .net c# stuff, patterns code and snippet help programmer.



      "Paul" wrote:
      Can you help me with this error?
      >
      string fields = string.Empty;
      ....
      fields.Length 0 ? fields += ", Name" : fields += "Name";
      >
      Error:
      Only assignment, call, increment, decrement, and new object expressions can be used as a statement
      >
      >

      Comment

      Working...