cannot convert from 'object' to system.array

G

Guest

I defined a session variable as an array using Session["MY_SESVAR"] = new
string[200];
Later, in my code, I need to set it and sort it.
I tried Session["MY_SESVAR"][j] = "some string";
My error is "Cannot apply indexing with [] to an expression of type 'object'.
Then, later, I try to use it, and sort on it an get 'cannot convert from
'object' to System.array.
what am I doing wrong here?
 
E

Egghead

You need to put the session variable to a string array, put your "some
string" to that string array, and put it back to the "MY_SESVR".
Egghead
 
B

Bruce Barker

the session collection only holds objects, to use an object as an array you
need to cast it:

((string[]) Session["MY_SESVAR"])[j] = "some string";

though this will still blow if there is no session, you should try something
like:

string[] myStrings = Session["MY_SESVAR"] as string[];
if (myStrings == null)
setupSession();
else
myStrings[j] = "some string";



-- bruce (sqlwork.com)
 
G

Guest

So, what you're saying is as follows:
First setup string array
string strArr[] = new string[200];
then, set it.
then, sort it.
then, take session variable and set it to strArr
Session["MY_VAR"] = strArr
??

Egghead said:
You need to put the session variable to a string array, put your "some
string" to that string array, and put it back to the "MY_SESVR".
Egghead
klynn said:
I defined a session variable as an array using Session["MY_SESVAR"] = new
string[200];
Later, in my code, I need to set it and sort it.
I tried Session["MY_SESVAR"][j] = "some string";
My error is "Cannot apply indexing with [] to an expression of type 'object'.
Then, later, I try to use it, and sort on it an get 'cannot convert from
'object' to System.array.
what am I doing wrong here?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top