Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 3rd, 2006, 01:45 PM
Friend of Wrox
 
Join Date: Feb 2006
Location: noida, UP, India.
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
Reply With Quote
  #2 (permalink)  
Old February 6th, 2006, 08:54 AM
Friend of Wrox
Points: 464, Level: 7
Points: 464, Level: 7 Points: 464, Level: 7 Points: 464, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: Colombo, , Sri Lanka.
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
Reply With Quote
  #3 (permalink)  
Old February 10th, 2006, 04:52 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 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.
Reply With Quote
  #4 (permalink)  
Old February 21st, 2006, 01:31 AM
Friend of Wrox
Points: 464, Level: 7
Points: 464, Level: 7 Points: 464, Level: 7 Points: 464, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2005
Location: Colombo, , Sri Lanka.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 10:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.