• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 366
  • Last Modified:

Setting a Cookie to an array of values in C#

I need to write a cookie from an array of strings. I see the userCookie is a dictionary. But how could I use it as an array?  I need it for something like this:

    HttpCookie userCookie = new HttpCookie("myStringArray");
    userCookie["cell0"] = strArray[0];
    userCookie["cell1"] = strArray[1];
    userCookie["cell2"] = strArray[2];


Thanks.
0
newbieweb
Asked:
newbieweb
1 Solution
 
Miguel OzSoftware EngineerCommented:
You can save it as string:
userCookie["myArray"] = String.Join(",", strArray);

then when you read it back you split it:
HttpCookie cookie = Request.Cookies["myStringArray"];
string s = cookie["myArray"]
string[] strArray= s.Split(',');


Check for read/write cookies:
http://www.java2s.com/Code/ASP/Session-Cookie/CreateandretrieveCookiedataC.htm
0
 
newbiewebSr. Software EngineerAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now