Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Class variables 2

Status
Not open for further replies.

vituja123

Programmer
Jun 16, 2005
110
0
0
US
Hi All,

I've been searching all through this forum to find out the best way to make/use Global variables accessible within my app based on a user session. Many people say use Session variables but there must be a better way.

I've tried created a class called GlobalClass but the values are always lost when moving from page to page (unless I declare the vars as shared but then they lose their session uniquiness).
------------------------------------------
Public Class GlobalClass

Public sqlConn As New SqlConnection(ConfigurationSettings.AppSettings("GlobalSQLDataConnection"))

Dim LogonClass As New LogonClass

Public Weekrange As String
Public WEEK_ID As String
Public DayDetail As String
Public SelectedEntryNum As Integer
Public SelectedDate As String
Public IframeHeight As Integer
Public Submitted As Boolean
...
end class
-----------------------------------

Other then using session variables,hidden fields, or querystrings, is there another way to share small amounts of data accross pages?
 
There is nothing wrong with using session variabales as long as you are not declaring tons and tons of them and as long as you are not storing large amounts of data it them, such as a large dataset.

Jim
 
Yeah, I'm not sure why you you are hesitant to use session variables. Variables can either be stored on the client or the server and there are arguments for and against both e.g.

# Storing on the client bloats the size of the page
# Storing them on the server uses more memory

Even if you created a global class that stored variables that were persistant between pages where do you think they would be stored? In server memory is the answer, so what's the difference between doing that and storing them in a session variable that will also store them in server memory?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
I know session vars can be handy but keeping globals in an easly reable area is appealing. Session vars can be set anywhere and don't require declearation. So if someone looks at my code, they'll have to read all the files to see my session vars.

I've started using properties. They seem to work fine as long as you are reading values. They keep their session states and can be seen through the entire app:

Public ReadOnly Property WEEKRANGE() As String
Get
Return "TEST"
End Get
End Property

But what if you want to both read and write them? Like Weekrange="1234".
 
You could always store your own class in a session variable...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Oh and to write to properties, don't use ReadOnly and use the Set method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi ca8msm,

If i want to use properties for both reading and writing, how do i set them up:

weekrange="1234"
X=weekrange

Public Property WEEKRANGE(ByVal VLU) As String
Set(ByVal VLU As String)

End Set
End Property
 
perfect example from the VS help:
Public Class Button
Private captionValue As String
Public Property Caption() As String
Get
Return captionValue
End Get
Set (ByVal Value As String)
captionValue = value
Repaint()
End Set
End Property
End Class
Based on class Button above, the following illustrates use of the Caption property:

Dim okButton As New Button()
okButton.Caption = "OK"' Invokes Set accessor
Dim s As String = okButton.Caption ' Invokes Get accessor
Jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top