Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Application Object Getters and Setters

I have had a hard time finding a newbie friendly example of accessing the Application Object. I have few things ( arrays ) I would like to store in the Application Object to keep as persistent data for about 2 hours until AppRecycle.

Anyhow, I know how to set an Application Object variable this way:

// One way
String[] users = new String[1000];
Application["users"] = users;

// Another way
Application.Add("users", users);

However I do not know how to access these variables once they are inside the Application Object. There is a Getter method Get. However it requires int index. The other method, Contents, get's everything, not just my array.

Here I try to retrieve my String[] array, but it gives me a error that I am trying to convert Object to String.

String[] usersTable = Application["users"];
// Since this is an object i also tried Application.users but gives error
like image 598
Sterling Duchess Avatar asked May 29 '26 01:05

Sterling Duchess


2 Answers

In Application you only save type "object". You need to cast to the desired type

string[] usersTable = (string[])Application["users"];
like image 55
Mihai Avatar answered May 31 '26 15:05

Mihai


You need to cast the value back to String[]:

String[] usersTable = (String[])Application["users"];

See How to: Read Values from Application State

like image 39
Cristian Lupascu Avatar answered May 31 '26 15:05

Cristian Lupascu