views:

259

answers:

2

hey all,

im having some problems with jagged arrays stored in session for ASP.net i have some code which creates a jagged array, them populates, and then stores this populated jagged array into session

protected string[][] answersJArray;
answersJArray[0] = new string[4]("test","test1","test2","test3"};
answersJArray[1] = new string[4]("test","test1","test2","test3"};
Session.Add("answersJArray", answersJArray);

how would i loop through each jagged array in the session ?? if they were not in session i no i could do the following

    for (j = 0; j < answersJArray[1].Length; j++)
    {
        label.Text = (answersJArray[1][j].ToString());
    }

how would i do the above by looping through the session ??

thanks

+1  A: 

First declare a jagged array variable and cast it from the session variable like so:

string[][] answersJArray = (string[][])Session["answersJArray"];

Then you can loop through the array like you were going to:

    for (j = 0; j < answersJArray[1].Length; j++)
    {
        label.Text = (answersJArray[1][j].ToString());
    }
Steve Danner
say i had an arraylist which is being stored in session, how would i acces this array list ?? so for example i have ... protected ArrayList arrAnswers = new ArrayList(); ... then i add some stuff to it ... then add it to the session, how would i access it again from session ??
c11ada
Very similarly to the way you access your jagged array. You add it with Session.Add("myArrayList", arrAnswers); Then access it by declaring an ArrayList and casting it from the session variable like this: ArrayList arrAnswers = (ArrayList)Session("myArrayList");
Steve Danner
+1  A: 

Shouldn't be too tough. You can get the object out of session and cast it into a string[]. If it's a local variable, you can check its length and iterate that way. I might be misunderstanding your issue, but am I missing something by saying get the variable out of the Session map, so you can work with it as a strong-type?

cbkadel
I also vote for Steve's answer too.
cbkadel