Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.

Saturday, September 22, 2007

Proper ViewState use and the coalescing operator

Here is a quick lesson the ViewState object for viewing an object's state. I found the use of the coalescing operator helpful for future use instead of wordy if then else statments.
  • public int CompanyID


    get { return (int)(ViewState["CompanyID"] ?? 0); }

    set { ViewState["CompanyID"] = value; }

The coalescing operator is a brother of if-then-else and
the more succinct: if (isX ? doA() : elseDoB).
I would describe coalescing as shorthand for: if (isX!=null) then getX() else getB().

No comments: