Tuesday, January 03, 2006

I wrote previously about a pattern using the coalescing operator (??) that I use to wrap ViewState:

public string Title
{
   
get { return (string)ViewState["title"] ?? "Default title"; }
   
set { ViewState["title"] = value; }
}

and

public string TitleID
{
    get { return (int?)ViewState["titleID"] ?? -1; }
   
set { ViewState["titleID"] = value; }
}

In the latter case, int is a value type and I thought the only way you could get things to work was to cast the ViewState to a nullable int type (int?), but there is a better solution:

public string TitleID
{
   
get { return (int)(ViewState["titleID"] ?? -1); }
   
set { ViewState["titleID"] = value; }
}

The difference is subtle but ViewState is an object type and you can apply the coalescing operator directly to it. Then cast that result to an int.

-Andrew

kick it on DotNetKicks.com   Tuesday, January 03, 2006 6:21:23 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 

I just came across these. Three good articles by Dino Esposito on building custom controls:

Building New Controls from the Ground Up:
http://msdn.microsoft.com/library/en-us/dnaspp/html/ContCrshCrsNew.asp

Building Data-Bound Controls:
http://msdn.microsoft.com/library/en-us/dnaspp/html/ContCrshCrsDB.asp

Deriving New Controls from Existing Classes:
http://msdn.microsoft.com/library/en-us/dnaspp/html/controlscrashcourse-deriving.asp

kick it on DotNetKicks.com   Tuesday, January 03, 2006 5:54:46 PM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  | 
 Sunday, January 01, 2006
My new blog.
kick it on DotNetKicks.com   Sunday, January 01, 2006 6:10:22 AM (Pacific Standard Time, UTC-08:00)  #    Disclaimer  |  Comments [0]  |