ArrayList

  • Thread starter Thread starter simon
  • Start date Start date
S

simon

First, I create arrayList and put it into a session.
(should I use arrayList to store different types of data into collection or should I use something else?)

ArrayList arrayValues = new ArrayList();
arrayValues[0] = lstMedia.SelectedValue;// this is int
arrayValues[1] = MediaCalendar.xSelectedDate.Date;//this is datetime
HttpContext.Current.Session["arrayValues"] = arrayValues;

Then, I want to change the value :
HttpContext.Current.session["arrayValues"][0] = 0;

and I get the following error:
Error 47 Cannot apply indexing with [] to an expression of type 'object'

Then I try to convert the object back into arrayList:

(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

and still the same error.

Any solution?

Thanks,S
 
simon said:
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

You're missing a pair of parens here:

((ArrayList)(HttpContext.Current.session["arrayValues"]))[0] = 0;



Oliver Sturm
 
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;
and still the same error.


Try this:
((ArrayList)(HttpContext.Current.session["arrayValues"]))[0] = 0;


Erick Sgarbi
www.blog.csharpbox.com



First, I create arrayList and put it into a session.
(should I use arrayList to store different types of data into collection or should I use something else?)

ArrayList arrayValues = new ArrayList();
arrayValues[0] = lstMedia.SelectedValue;// this is int
arrayValues[1] = MediaCalendar.xSelectedDate.Date;//this is datetime
HttpContext.Current.Session["arrayValues"] = arrayValues;

Then, I want to change the value :
HttpContext.Current.session["arrayValues"][0] = 0;

and I get the following error:
Error 47 Cannot apply indexing with [] to an expression of type 'object'

Then I try to convert the object back into arrayList:

(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

and still the same error.

Any solution?

Thanks,S
 
Simon, sounds like you'd benefit from reading a few quickstarts and
tutorials. You'll usually get more help in technical forums such as this one
if you give an indication that you have made some effort to learn the
subject area.

Have a look here...

http://samples.gotdotnet.com/quickstart/howto/

and try out some of the samples. If you still get stuck, try Googling for
what it is you want to do, if you can't easily find an answer to your
question, post back.

--
Regards,

Tim Haughton

Agitek
http://agitek.co.uk
http://blogitek.com/timhaughton
 
I am not the best at C# nonetheless, i have used ListDictionary for collections. I have sometimes created my own "listitem" classes where i just store them as objects.
i would like to know the answer too

try this

((ArrayList)(HttpContext.Current.session["arrayValues"]))[0]=0;

i think u are not casting properly i could be worng.

r


First, I create arrayList and put it into a session.
(should I use arrayList to store different types of data into collection or should I use something else?)

ArrayList arrayValues = new ArrayList();
arrayValues[0] = lstMedia.SelectedValue;// this is int
arrayValues[1] = MediaCalendar.xSelectedDate.Date;//this is datetime
HttpContext.Current.Session["arrayValues"] = arrayValues;

Then, I want to change the value :
HttpContext.Current.session["arrayValues"][0] = 0;

and I get the following error:
Error 47 Cannot apply indexing with [] to an expression of type 'object'

Then I try to convert the object back into arrayList:

(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

and still the same error.

Any solution?

Thanks,S
 
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

that code is really problematic and error prone. the correct pattern is to
extract the object, test it for validity and then proceed

if(Session["item"] != null)
{
ArrayList arr = Session["item"] as ArrayList;
if(arr != null && arr.Count > 0)
{
//then index into the container
}
}


--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
-------------------------------------------------------------------------------


Oliver Sturm said:
simon said:
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;

You're missing a pair of parens here:

((ArrayList)(HttpContext.Current.session["arrayValues"]))[0] = 0;



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Alvin said:
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;


that code is really problematic and error prone.

I absolutely second that, but I was only pointing out the precise
problem that leads to the OP's compile time error.

the correct pattern is to
extract the object, test it for validity and then proceed

if(Session["item"] != null)
{
ArrayList arr = Session["item"] as ArrayList;
if(arr != null && arr.Count > 0)
{
//then index into the container
}
}

Right. You can leave out the outer if, though:

ArrayList arr = Session["item"] as ArrayList;
if(arr != null && arr.Count > 0) {
//then index into the container
}

The magic of the "as" operator makes it work.



Oliver Sturm
 
k

--
Regards
Alvin Bruney
[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
available at www.lulu.com/owc, Amazon, B&H etc
-------------------------------------------------------------------------------


Oliver Sturm said:
Alvin said:
(ArrayList)(HttpContext.Current.session["arrayValues"] )[0] = 0;


that code is really problematic and error prone.

I absolutely second that, but I was only pointing out the precise problem
that leads to the OP's compile time error.

the correct pattern is to extract the object, test it for validity and
then proceed

if(Session["item"] != null)
{
ArrayList arr = Session["item"] as ArrayList;
if(arr != null && arr.Count > 0)
{
//then index into the container
}
}

Right. You can leave out the outer if, though:

ArrayList arr = Session["item"] as ArrayList;
if(arr != null && arr.Count > 0) {
//then index into the container
}

The magic of the "as" operator makes it work.



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 
Back
Top