unable to evaluate viewState against Null
-
27-06-2021 - |
سؤال
This method I am using to fill the data source for a grid view, but for when getnew
is false
, it won't return any value , just returns a list with a single null value in it.
private List<T> GetAll_T(bool getNew)
{
if (getNew)
return (ViewState["T"] = Get_T()) as List<T>;
//Get_T() returns a CustomList
return new List<T>
{
ViewState["T"] != null ?
ViewState["T"] as T:
(T)(ViewState["T"] = Get_T()) Collection
};
}
it gives me a warning for second line[when view state is null].expression is always false
why there is warning, when it's logically correct !
المحلول
It is not clear from your code snippet what CustomerService.GetAllCustomer()
returns.
You use it both like it returns a list on line(2) of the function and like it returns a single object on line(8).
I suggest writing it like this
private List<CustomerMaster> GetAllCustomer(bool getNew)
{
if (getNew || null == ViewState["CustomerDataset"])
ViewState["CustomerDataset"] = CustomerService.GetAllCustomer();
return ViewState["CustomerDataset"] as List<CustomerMaster>;
}
لا تنتمي إلى StackOverflow