|
Subject:
|
How to declare global variable
|
|
Posted By:
|
gaurav_jain2403
|
Post Date:
|
2/3/2006 12:45:41 PM
|
Hello friends. I want to know how we can declare a variable as global so that we can use it throughout the application in VB.NET and ASP.NET. Is it possible to assign it some value like 0("zero") and then changing its value in our code at runtime.
|
|
Reply By:
|
nalla
|
Reply Date:
|
2/6/2006 7:54:05 AM
|
Hi gaurav_jain2403,
In VB.Net Windows application you can have global variables as Public. In Web application you can't have global variables. Actually you don't need global variables if you do the design properly.
But it's better if you read some articles in session management. just google.
nalaka hewage
|
|
Reply By:
|
Imar
|
Reply Date:
|
2/10/2006 3:52:13 PM
|
Hi there,
While I agree that having global variables is a good thing to avoid, I don't agree you can't have them in a Web app. Add the following to the Code Behind of your Global.asax file:namespace Your.Namespace
{
public class Global : System.Web.HttpApplication
{
private static int someVariable = 0;
public static int SomeVariable
{
get
{
return someVariable;
}
set
{
someVariable = value;
}
}
// Rest of Global class here
}
}This sets up a shared (static) property for the entire application.
You can then use it like this in a page:
Your.Namespace.Global.SomeVariable = SomeValue;
Notice that this code isn't thread-safe, so you'll likely run into synchronization issues between threads. You'll need to write additional code to make this work well...
Imar --------------------------------------- Imar Spaanjaars Everyone is unique, except for me.
|
|
Reply By:
|
nalla
|
Reply Date:
|
2/21/2006 12:31:59 AM
|
Hi Imar, That's very interesing. thanx for the information.
nalaka hewage
|