Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2006, 01:45 PM
Friend of Wrox
 
Join Date: Feb 2006
Posts: 133
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to gaurav_jain2403
Default How to declare global variable

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.
__________________
Gaurav
 
Old February 6th, 2006, 08:54 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


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
 
Old February 10th, 2006, 04:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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:
Code:
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.
 
Old February 21st, 2006, 01:31 AM
Friend of Wrox
 
Join Date: Dec 2005
Posts: 132
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nalla Send a message via Yahoo to nalla
Default


Hi Imar,
That's very interesing.
thanx for the information.


nalaka hewage





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
Must declare the scalar variable kofibull Visual Studio 2005 0 November 24th, 2006 05:47 PM
"Must declare the scalar variable" Orchid85 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 1 September 22nd, 2006 10:56 AM
declare a variable without setting a value to it crmpicco Javascript How-To 1 July 18th, 2005 07:25 PM
declare empty variable jtyson General .NET 2 April 8th, 2004 11:02 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.