0

I am using ArrayList in Asp.net I want to extract specific items . My code is

ArrayList items = (ArrayList)Session["mycart"];
foreach(var v in items)
{



}

but this is not working . I want to get value like

v.myvalue;

My arralist is filled with several items coming from prevoius page.

2

1 Answer 1

3

The issue is that ArrayList stores all elements as object. You need to perform a cast to the type of object that contains myvalue. For example

ArrayList items = (ArrayList)Session["mycart"];
foreach(var v in items)
{
     MyObject o = v as MyObject;
     if (o != null)
     {
         // do stuff with o.myvalue
     }
}

It may be better to just use the generic List class rather ArrayList, although you may have a perfectly reason for doing otherwise. Generally, you should use the generic (e.g. List<MyObject>), not only for performance but also ease of use.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly I changed ArrayList to generic List and now its working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.