J
Jonathan Wood
Greetings,
I often find myself writing public properties in my ASP.NET code and I need
the ability to robustly handle cases where the property has not been set.
So I often end up with code like this:
public Guid PresenterID
{
get
{
object obj = ViewState["PresenterID"];
if (obj == null)
return null;
return (Guid)obj;
}
set { ViewState["PresenterID"] = value; }
}
My get seem overly complex because if I typecast the result of ViewState, I
get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.
I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?
Thanks.
Jonathan
I often find myself writing public properties in my ASP.NET code and I need
the ability to robustly handle cases where the property has not been set.
So I often end up with code like this:
public Guid PresenterID
{
get
{
object obj = ViewState["PresenterID"];
if (obj == null)
return null;
return (Guid)obj;
}
set { ViewState["PresenterID"] = value; }
}
My get seem overly complex because if I typecast the result of ViewState, I
get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.
I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?
Thanks.
Jonathan