none
If elase syntax RRS feed

  • Question

  • Hello,

    This ?: syntax is wrong? why?

    This is similar :

    condition ? first_expression : second_expression;  


    Thanks

    Monday, January 15, 2018 9:50 PM

Answers

  • I think that ‘?:’ is acceptable, but C# does not allow such kind of statements (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0201). You can make a valid statement:

       var unused = txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);

    Of course, you can also write:

       btnClear.Enabled = txt_Salary.Text.Length == 0 ? false : true;

    or

       btnClear.Enabled = txt_Salary.TextLength != 0;

    Monday, January 15, 2018 10:17 PM
  • Hi Arash_89,

    Adding with Viorel_'s answer when you use "?" you need a variable that holds the output result. Like following, see the bold part. Hope this helps you.

    var returnResult = txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);

    Or,

    directly put with return statement like,

    return txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, January 16, 2018 2:01 PM
    Moderator

All replies

  • I think that ‘?:’ is acceptable, but C# does not allow such kind of statements (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0201). You can make a valid statement:

       var unused = txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);

    Of course, you can also write:

       btnClear.Enabled = txt_Salary.Text.Length == 0 ? false : true;

    or

       btnClear.Enabled = txt_Salary.TextLength != 0;

    Monday, January 15, 2018 10:17 PM
  • Thank you,

    But what is difference between if-else and ?: syntax?

    In this example and if-else syntax we don't have return value but in ?: operator we have return value.

    if (txt_Salary.Text.Length == 0)
    {
        btnClear.Enabled = false;}
              else
             {
                   btnClear.Enabled = true;
               }
    }
    
    =================
    
    (txt_Salary.Text.Length == 0) ? btnClear.Enabled = false : btnClear.Enabled = true;

    Monday, January 15, 2018 10:33 PM
  • In my opinion, ‘if’ is a statement that cannot be used as an expression, and ‘?:’ is an expression that cannot be used in C# as a statement.

    • Proposed as answer by SimonRev Monday, January 15, 2018 10:57 PM
    Monday, January 15, 2018 10:47 PM
  • Hi Arash_89,

    Adding with Viorel_'s answer when you use "?" you need a variable that holds the output result. Like following, see the bold part. Hope this helps you.

    var returnResult = txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);

    Or,

    directly put with return statement like,

    return txt_Salary.Text.Length == 0 ? (btnClear.Enabled = false) : (btnClear.Enabled = true);


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Tuesday, January 16, 2018 2:01 PM
    Moderator
  • Hi Arash_89,

    If you have solved your issue, please remember to close your thread by marking the helpful post as answer, If not, please take a look this link about If else and ?:

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 26, 2018 7:56 AM