I have declared a public variable as follows:
Public Class xxxxx
Inherits System.Web.UI.Page
Public m_ID as Integer
In a certain Private Sub m_ID is set to 5.
In another Private Sub, suddenly m_ID is 0??, while there were no changes at all ??
How can ?
Help is appreciated, Ger.
Hi,
if those subs are running within different (subsequent) postbacks, reason is that Page class is recreated for each and every request which causes normal local members to be cleared to their defaults (initial request and subsequent postback would be 2 requests and 2 separate Page class instances). That's how Page class actually is stateless by default.
If you want something to be preserved over postbacks, you need to create a property which utilizes Page's ViewState collection.
Public Property m_ID() As Integer
Get
If Not ViewState("m_ID") Is Nothing Then
Return CInt(ViewState("m_ID"))
End If
Return 0
End Get
Set(Value As Integer)
ViewState("m_ID") = Value
End Set
End Property
Now, you could get/set its value like members, but difference being that via this setup, the property keeps its value for subsequent postbacks.
Thank you Teemu Keiski,
I implemented your solution and it works great.
Now I learn of lot more of Web-applications...
Regards, Ger.
Hi Teemu Keiski,
I have implemented your solution within a class, but, when I do it outside a class I got the error:
Statement is not valid in a namespace.
Where I have to put the code Public Property.... etc, so everywhere in the whole application I can retrieve the value of the variable ??
Thanks, Ger.
It needs to be in a class, but the proeprty can be static (Shared keyword in VB) when you could use it via the class's type name without instantiation
E.g
Public Class Helpers
Public Shared Property Demo ...
...
End Property
End Class
When you'd use itHelpers.Demo
I have a desparate desire to VB6 and earlier, where it was simple to declare a public variable, which can be used throughout the
whole application......
When I use this code in VB.net (as you suggest):
Public Shared Property m_Gebr() as String
Get
if not viewstate etc. etcetera
I got the error: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Is there a simple solution to use a PUBLIC variable throughout the application, whether a form has a postback or not.
(Sorry for being so negative, but it takes hours and hours to come to a solution (and I am so impatient(lol)))....
Regards, Ger.
Application variables or session variables are probably your best bet if you are just looking for a global variable to pass around.
If you declare the variable on the page within the script blocks, but not inside a subroutie you can use that variable anywhere on the page. HOWEVER. You need to realize the that as soon as you post back you are going to lose the value.
You need to either make a hidden control to hold the value, use the viewstate like recommended earlier, or put the value into a session variable. Maybe using the session variables will do what you want.
Joshua
Well, remember that web application is stateless whereas VB6 client application is not (there's the form instance etc always keeping state, and ViewState emulates that)
Then it is just that when the variable/property is updated and when read. Yes, you cannot access ViewState if the proeprty is shared(because ViewState is page & control instance specific object).
You could have
Public Class Helpers
Private myVar As String
Public Shared Property Something() As String
Get
return myVar
End get
Set(ByVal value As String)
myVar= Value
End Set
End Property
End Class
Then you just need note that setting this has impact on every part of the code reading the property (e.g concurrency stuff) e.g every Page class instance ect etc (Page class is recreated for every request)
And to set something globally, you can use Application or Cache collection in ASP.NET straight away.
http://www.dotnetjunkies.com/quickstart/aspplus/doc/stateoverview.aspx
If you are looking to getting configuration globally (read-only manner), you could use web.config to store application settings which you read with System.Configuration.ConfigurationSettings class
http://www.dotnetjunkies.com/quickstart/aspplus/doc/configretrieve.aspx
0 comments:
Post a Comment