Public Arrays lose values

TheWizardofInt

Well-known member
Joined
Dec 31, 1969
Messages
333
Location
Orlando, FL
I am obviously doing this wrong

I will declare an array as public in the class for the form I am using on the page

Within a sub procedure that is also public within the class, I will redim the array and fill it with information

The next time I call the array, it has a value of nothing.

Any idea why?
 
rediming an array clears it, im not sure about aspx, but preserve keyword after the the redim in vb leaves the values

like, redim preserve .. etc etc as u normaly would
 
When you call the array (the next time) is it after Posting the page?
If not, then follow Little3Lues advice.
 
I create the array in the PageLoad and it is fine until the next event, which seems to wipe out the array

The preserve command didnt work. I also tried Friend sRecArr as ArrayList and that didnt keep it from being wiped out either

It doesnt go back through the page load after the first time, and I am using the code behind method

Thanks
 
Any objects or variables you declare are only stored on the servers memory until the page finishes loading. You have to remember that ASP.NET is a web language, and should be treated as such. Its not like an application (unless you actually specify it to be). If you want you can store objects in memory permanently for re-use. This is dangerous though if not used properly, because you can suck up a lot of memory on your server if a ton of users access that page.

A few ways to keep objects in memory (or pass pass them along);
- Use application state (look up the Application object).
- Use the cache (look up Cache object).
- Cookies (look up HttpCookie object).
- Sessions state (look up Session object).
- View state (look up ViewState object).

The session state or view state is probably your best option.
 
Last edited by a moderator:
Back
Top