Last post Nov 01, 2016 01:19 PM by PatriceSc
Nov 01, 2016 10:49 AM|rdavidd|LINK
I am trying to understand how to establish a static variable in a page's class that is local to the class and not shared across instances of the page.
Partial Class ViewPage
Dim ABC as String - this is local to the class but is not static and is reinitialized each time the page is reloaded
Shared ABC as String - This is static but it is shared with all instances of the class
Static ABC as String - This causes an error saying "Static is not valid on a member variable declaration"
Is there a variable declaration that is valid for a local static variable declared at the page class level?
Nov 01, 2016 10:57 AM|Siva Krishna Macha|LINK
I think, you would like to maintain the value of the variable across the postbacks (button clicks or dropdown change events, etc). If so, then you are probably looking for ViewState which can persist the values across the postbacks in a given page.
To read more about ViewState: http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State
Nov 01, 2016 11:23 AM|rdavidd|LINK
Thanks you for your assistance! I am familiar with cookie, session, and viewstate storage and am using them (mostly session items) to store static "variables" but I have a few items that are multi-dimensional arrays [ie: myarray(x, y, z)] which can contain
a large number of x values an up to 25 or more y and z values ... I understand how to establish a class with a single list of multiple items to store and retrieve from the viewstate, but I don't quite understand how I could easily place multi-dimensional array
in a session or viewstate "variable" ... maybe it's a little more complicated than I though ... I was hoping there was a variable declaration like "Static" that would establish a local static value.
Nov 01, 2016 11:32 AM|rdavidd|LINK
One of the things I have considered is to declare the multi-dimensional arrays as "Public Shared" and add an additional parameter to each with a top bound of 1 to hold the session ID as an identifier. Then each instances' data could be identified by the
session ID when retrieved.
Nov 01, 2016 11:42 AM|mgebhard|LINK
but I don't quite understand how I could easily place multi-dimensional array in a session or viewstate "variable" ... maybe it's a little more complicated than I though ...
You just assign the object to ViewState like any another object. However, the array items must be serializable.
I was hoping there was a variable declaration like "Static" that would establish a local static value.
Static or Shared member fields are global to the application. This is a construct of the language.
It would be a lot easier to assist you if you explained the problem, provided error messages and source code rather than asking the community to solve a problem that might not be the right direction.
Nov 01, 2016 12:51 PM|rdavidd|LINK
There are no errors or source code at this point since i am just looking at all the possibilities. What I am trying to do is find a way to put values in an array during page routines, maintain those array values when a page posts back so other routines can
access the values in the array, but not share the array values with other instances of the page. But from what you are saying, that is not possible with the way .NET is configured to handle all static variables in a class as global.
Nov 01, 2016 01:19 PM|PatriceSc|LINK
It's just that by nature shared variables are class level variables shared by all instances which is not what you want. Which kind of value will you store there? How are they changed? Depending on how you handle those values you could use the viewstate (or
an explicit hidden field). Maybe also a database (especially if you keeping those values accross session might be a useful user feature).