By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,900 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

Casting session object into a 2-dimensional array

P: n/a
Hello All,

I have a 2-dimensional array that I am storing as a session variable. I have
no idea on how I can cast the session variable back to 2-dimensional array.
Any pointers?

Reference code below...

Array declaration:
DateTime[][] DateRangesForDataLists = new DateTime[5][];

Sample element population:
DateRangesForDataLists[0] = new DateTime[2];
DateRangesForDataLists[0][0] = System.DateTime.Today;
DateRangesForDataLists[0][1] = new
DateTime(System.DateTime.Today.Year,System.DateTim e.Today.Month,1);

Storing in session:
Session["DataListStartEndDates"] = DateRangesForDataLists;

Trying to access from session:
private DateTime GetDateFromSession(int FirstIndex, int SecondIndex)
{
DateTime[,] DateRangesForDataLists = new DateTime[5,2];
DateRangesForDataLists = (DateTime[,])Session["DataListStartEndDates"];
return DateRangesForDataLists[FirstIndex,SecondIndex];
}

And in the above method it is giving me an error: "Specified cast is not
valid." What am I doing wrong?
Thanks for your help!!

Jan 10 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try:

DateTime[][] DateRangesForDataLists =
(DateTime[][])Session["DataListStartEndDates"];
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hello All,

I have a 2-dimensional array that I am storing as a session variable. I
have
no idea on how I can cast the session variable back to 2-dimensional
array.
Any pointers?

Reference code below...

Array declaration:
DateTime[][] DateRangesForDataLists = new DateTime[5][];

Sample element population:
DateRangesForDataLists[0] = new DateTime[2];
DateRangesForDataLists[0][0] = System.DateTime.Today;
DateRangesForDataLists[0][1] = new
DateTime(System.DateTime.Today.Year,System.DateTim e.Today.Month,1);

Storing in session:
Session["DataListStartEndDates"] = DateRangesForDataLists;

Trying to access from session:
private DateTime GetDateFromSession(int FirstIndex, int SecondIndex)
{
DateTime[,] DateRangesForDataLists = new DateTime[5,2]
DateRangesForDataLists = (DateTime[,])Session["DataListStartEndDates"];
return DateRangesForDataLists[FirstIndex,SecondIndex];
}

And in the above method it is giving me an error: "Specified cast is not
valid." What am I doing wrong?
Thanks for your help!!

Jan 10 '06 #2

P: n/a
Nope, it gives me the same error message.

When you observe my code, I declared the array as a jagged array but while
retrieving I am casting it to a normal 5x2 array.....is that the problem?

How can I cast it into a jagged array?

"Karl Seguin [MVP]" wrote:
Try:

DateTime[][] DateRangesForDataLists =
(DateTime[][])Session["DataListStartEndDates"];
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hello All,

I have a 2-dimensional array that I am storing as a session variable. I
have
no idea on how I can cast the session variable back to 2-dimensional
array.
Any pointers?

Reference code below...

Array declaration:
DateTime[][] DateRangesForDataLists = new DateTime[5][];

Sample element population:
DateRangesForDataLists[0] = new DateTime[2];
DateRangesForDataLists[0][0] = System.DateTime.Today;
DateRangesForDataLists[0][1] = new
DateTime(System.DateTime.Today.Year,System.DateTim e.Today.Month,1);

Storing in session:
Session["DataListStartEndDates"] = DateRangesForDataLists;

Trying to access from session:
private DateTime GetDateFromSession(int FirstIndex, int SecondIndex)
{
DateTime[,] DateRangesForDataLists = new DateTime[5,2]
DateRangesForDataLists = (DateTime[,])Session["DataListStartEndDates"];
return DateRangesForDataLists[FirstIndex,SecondIndex];
}

And in the above method it is giving me an error: "Specified cast is not
valid." What am I doing wrong?
Thanks for your help!!


Jan 10 '06 #3

P: n/a

"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hello All,

I have a 2-dimensional array that I am storing as a session variable. I
have
no idea on how I can cast the session variable back to 2-dimensional
array.
Any pointers?

Reference code below...

Array declaration:
DateTime[][] DateRangesForDataLists = new DateTime[5][];


Hmm, that isn't a 2-dimensional array...rather an array of arrays...thought
I'd point that out. A two dimensional array would be defined as DateTime[,]
DateRangesForDataLists, would it not? I may be wrong, haven't dealth with
2-dimensional arrays in a long time in C#....in VB.Net, though, DateTime()()
is considered an array of arrays and DateTime(,) is 2-dimensional...

Check it out, remember, I am hardly ever correct in my assumptions... :)
Especially, this early in the morning...

Mythran

Jan 10 '06 #4

P: n/a
how is it declared? why can't you cast it as it is declared? post some code
if you still need help.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:#G**************@TK2MSFTNGP12.phx.gbl...
Try:

DateTime[][] DateRangesForDataLists =
(DateTime[][])Session["DataListStartEndDates"];
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
Hello All,

I have a 2-dimensional array that I am storing as a session variable. I
have
no idea on how I can cast the session variable back to 2-dimensional
array.
Any pointers?

Reference code below...

Array declaration:
DateTime[][] DateRangesForDataLists = new DateTime[5][];

Sample element population:
DateRangesForDataLists[0] = new DateTime[2];
DateRangesForDataLists[0][0] = System.DateTime.Today;
DateRangesForDataLists[0][1] = new
DateTime(System.DateTime.Today.Year,System.DateTim e.Today.Month,1);

Storing in session:
Session["DataListStartEndDates"] = DateRangesForDataLists;

Trying to access from session:
private DateTime GetDateFromSession(int FirstIndex, int SecondIndex)
{
DateTime[,] DateRangesForDataLists = new DateTime[5,2]
DateRangesForDataLists = (DateTime[,])Session["DataListStartEndDates"];
return DateRangesForDataLists[FirstIndex,SecondIndex];
}

And in the above method it is giving me an error: "Specified cast is not
valid." What am I doing wrong?
Thanks for your help!!


Jan 10 '06 #5

P: n/a
I'm not sure why my code doesn't work, this code compiles fine for me:

DateTime[][] DateRangesForDataLists = new DateTime[2][];
DateRangesForDataLists[0] = new DateTime[2];
DateRangesForDataLists[0][0] = DateTime.Today;
DateRangesForDataLists[0][1] = DateTime.Today.AddYears(1);

DateRangesForDataLists[1] = new DateTime[3];
DateRangesForDataLists[1][0] = DateTime.Today;
DateRangesForDataLists[1][1] = DateTime.Today.AddYears(1);
DateRangesForDataLists[1][2] = DateTime.Today.AddYears(2);

object o = DateRangesForDataLists;

DateTime[][] back = (DateTime[][])o;

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:DB**********************************@microsof t.com...
Nope, it gives me the same error message.

When you observe my code, I declared the array as a jagged array but while
retrieving I am casting it to a normal 5x2 array.....is that the problem?

How can I cast it into a jagged array?

"Karl Seguin [MVP]" wrote:
Try:

DateTime[][] DateRangesForDataLists =
(DateTime[][])Session["DataListStartEndDates"];
karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Diffident" <Di*******@discussions.microsoft.com> wrote in message
news:BA**********************************@microsof t.com...
> Hello All,
>
> I have a 2-dimensional array that I am storing as a session variable. I
> have
> no idea on how I can cast the session variable back to 2-dimensional
> array.
> Any pointers?
>
> Reference code below...
>
> Array declaration:
> DateTime[][] DateRangesForDataLists = new DateTime[5][];
>
> Sample element population:
> DateRangesForDataLists[0] = new DateTime[2];
> DateRangesForDataLists[0][0] = System.DateTime.Today;
> DateRangesForDataLists[0][1] = new
> DateTime(System.DateTime.Today.Year,System.DateTim e.Today.Month,1);
>
> Storing in session:
> Session["DataListStartEndDates"] = DateRangesForDataLists;
>
> Trying to access from session:
> private DateTime GetDateFromSession(int FirstIndex, int SecondIndex)
> {
> DateTime[,] DateRangesForDataLists = new DateTime[5,2]
> DateRangesForDataLists = (DateTime[,])Session["DataListStartEndDates"];
> return DateRangesForDataLists[FirstIndex,SecondIndex];
> }
>
> And in the above method it is giving me an error: "Specified cast is
> not
> valid." What am I doing wrong?
>
>
> Thanks for your help!!
>


Jan 10 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.