cannot convert from 'object' to system.array

Discussion in 'ASP .Net' started by =?Utf-8?B?a2x5bm4=?=, May 25, 2005.

  1. 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?
    =?Utf-8?B?a2x5bm4=?=, May 25, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?a2x5bm4=?=

    Egghead Guest

    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" <> wrote in message
    news:...
    > 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?
    Egghead, May 25, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?a2x5bm4=?=

    Bruce Barker Guest

    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)


    "klynn" <> wrote in message
    news:...
    >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?
    Bruce Barker, May 25, 2005
    #3
  4. 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" wrote:

    > 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" <> wrote in message
    > news:...
    > > 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?

    >
    >
    >
    =?Utf-8?B?a2x5bm4=?=, May 25, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simon Harris
    Replies:
    2
    Views:
    2,587
    Simon Harris
    Apr 27, 2005
  2. Simon Harris
    Replies:
    0
    Views:
    733
    Simon Harris
    Apr 27, 2005
  3. Patrick Olurotimi Ige
    Replies:
    3
    Views:
    8,671
    Patrick Olurotimi Ige
    May 4, 2005
  4. Paulo
    Replies:
    2
    Views:
    5,757
  5. Robert
    Replies:
    5
    Views:
    894
    阿利
    Apr 20, 2006
Loading...

Share This Page