Reputation: 435
(List<Fruit>)Session["listSession"]
the session list is created in my home page. and i would like to access information on another page
I would like to loop throw
if ((List<Fruit>)Session["listSession"].name == "apple ")
{
item.(access a method in my fruit class)
}else {
// do something else
}
\
Upvotes: 1
Views: 278
Reputation: 45083
A couple of points here: you can just grab the instance from the session as
a list and keep a reference to it, then you can check it is something (not null
) and that it contains something which is also something (if nullable), before grabbing a reference of that and performing desired actions:
var fruitList = Session["listSession"] as List<Fruit>;
if (fruitList != null && fruitList.Count > 0)
{
var fruit = fruitList[0];
if (fruit != null && fruit.name == "apple ")
{
fruit.Consume();
}
}
That ought to help, though I'm sure you'll need to build on it to further your purpose.
Upvotes: 2
Reputation: 4412
List<Fruit> fruits = Session["listSession"] as List<Fruit>;
if(fruits != null)
{
foreach(Fruit fruit in fruits)
{
if(fruit.name=="apple")
fruit.Method();
else
//do something else
}
}
Upvotes: 4