-5

I have a string array like this

string[] BranchIds = {"1","2","3"};

and then stored it into a session

Session["BranchIds"] = BranchIds;

Now how can I retrieve this values as integers from session?

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Sribin
  • 307
  • 4
  • 16

3 Answers3

1

You can do this:

string[] branchIds = (string[])Session["BranchIds"]

Then you can iterate array for it's values.

Diabolus
  • 268
  • 2
  • 15
1

You first get the values as an array of strings:

var strArr = (string[])Session["BranchIds"];

Then you can convert it to an array of int:

var intArr = Array.ConvertAll(strArr, int.Parse);

Barry O'Kane
  • 1,189
  • 7
  • 12
1

The 2 Question are not related, so retrieve from session is same as you add to session so if say:

string [] BranchIds = (string[])Session["BranchIds"]

then you you retrieved the array from session.

to parse a string value to integer you can use:

Int32.Parse()

so for example to parse first id:

int id = Int32.Parse(BranchIds[0]);

or you can do a for loop and convert all of them to integers, or you can store to session in integer from beginning if needed.

mohamed-ibrahim
  • 10,837
  • 4
  • 39
  • 51