I want to retain a variable between postbacks, so I write an accessor to put it into viewstate. Which one of these is the best way to access it? Or is there a better option?
Option 1:
private int Status
{
get
{
try
{
return (int)ViewState[@"__Status"];
}
catch
{
return 0;
}
}
set
{
ViewState[@"__Status"] = value;
}
}
Option 2:
private int Status
{
get
{
if (ViewState[@"__Status"] is int)
{
return (int)ViewState[@"__Status"];
}
else
{
return 0;
}
}
set
{
ViewState[@"__Status"] = value;
}
}
Thanks
Edit: I'm using C# 2.0