Makieng my variable Global

H

Husam

Hi EveryBody:

How can I make my variable as global or variable that declared in all my
entire project.

For Example in Desktop application you can add your variable to the Module
to make declared for the entire project.

How can I do So in my web Application?

any help will be appreciated

regard's

Husam
 
P

Phil Johnson

You can use the application session state for variables you want available
accross sessions (different users) and the session state for variables you
want available across a session (i.e. can be different values for different
users)

To use them, syntax is as follows:

To store a value in application state:

string GlobalVariable = "Some Value";
Application["GlobalVariable"] = GlobalVariable;

To retrieve a value from application state:

string RetrievedVariable = Convert.ToString(Application["GlobalVariable"]);

Session is the same syntax, but replace Application with Session.

Always remember that you can store different types in state, but they will
always be returned from state as an object, so you need to unbox them (i.e.
convert them to the type you put in there.

Also, its good practice to check the state variable is not null before
trying to retrieve it.

Hope that helps.
--
Regards,

Phillip Johnson (MCSD For .NET)
PJ Software Development
www.pjsoftwaredevelopment.com
 
J

Jose A. Fernandez

Any class with static property is global for all session, and add
intellisense to Application variables.
Just create a class
Example:
Public Class Util
Public Shared Function AppPath() As String
Return System.AppDomain.CurrentDomain.BaseDirectory()
End Function

Public Shared Property ExampleString() As String
Get
If HttpContext.Current.Application("ExampleString") IsNot
Nothing Then
Return
HttpContext.Current.Application("ExampleString")
Else
Return String.Empty
End If
End Get
Set(ByVal value As String)
HttpContext.Current.Application("ExampleString") = value
End Set
End Property

End Class


______________________
Jose A. Fernandez
blog: http://geeks.ms/blogs/fernandezja
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top