locked
What is the long form of this int? and the long form "sdate_asc" ? "sdate_desc" : "sdate_asc" RRS feed

  • Question

  • User2130491911 posted

    Hi All,

    I do not like the question mark operator it makes the code hard to read. I would like to know what the long form of the operator is:

    Here are 3 examples:

    ViewBag.sDateSortParm = SortOrder == "sdate_asc" ? "sdate_desc" : "sdate_asc";

    and 

      public ActionResult Index(string SortOrder, int? SearchString, int? CurrentFilter, int? Page)

    and 

                int pageNumber = (Page ?? 1);

    How can I translate these so that I don't have to use the meaningless and bloody awful question make?

    Many thanks

    Monday, January 9, 2017 8:18 PM

Answers

  • User2103319870 posted

    AppDev01

    ViewBag.sDateSortParm = SortOrder == "sdate_asc" ? "sdate_desc" : "sdate_asc";

    Here you are using conditional operator and This can be written with an if statement like 

    if (SortOrder == "sdate_asc")
                {
                    ViewBag.sDateSortParm ="sdate_desc";
                }
                else
                {
                    ViewBag.sDateSortParm ="sdate_asc"; 
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 9, 2017 8:29 PM
  • User2103319870 posted

    int pageNumber = (Page ?? 1);

    Here you are using null-coalescing operator, this will Set pageNumber to the value of Page if Page is NOT null; otherwise, if page = null, set pageNumber  to 1.

    This can be rewritten like below

    int pageNumber = 0;
                if (Page != null)
                {
                    pageNumber = Page;
                }
                else
                {
                    pageNumber = 1;
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 9, 2017 8:37 PM

All replies

  • User2103319870 posted

    AppDev01

    ViewBag.sDateSortParm = SortOrder == "sdate_asc" ? "sdate_desc" : "sdate_asc";

    Here you are using conditional operator and This can be written with an if statement like 

    if (SortOrder == "sdate_asc")
                {
                    ViewBag.sDateSortParm ="sdate_desc";
                }
                else
                {
                    ViewBag.sDateSortParm ="sdate_asc"; 
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 9, 2017 8:29 PM
  • User2103319870 posted

    int pageNumber = (Page ?? 1);

    Here you are using null-coalescing operator, this will Set pageNumber to the value of Page if Page is NOT null; otherwise, if page = null, set pageNumber  to 1.

    This can be rewritten like below

    int pageNumber = 0;
                if (Page != null)
                {
                    pageNumber = Page;
                }
                else
                {
                    pageNumber = 1;
                }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 9, 2017 8:37 PM
  • User2130491911 posted

    Many man thanks for the help!

    Monday, January 9, 2017 9:51 PM