473,326 Members | 2,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How to make a variable global?

I want to take a global variable in my ASP.NET application. How do I
make it global so that it is accessible to all the pages in my
application? Do I put it in the Global.asax file? If so, Global.asax
now has a class Global and so if I put it there it won't be global
anymore. It'll be a class member. Then how do I access it?

Nov 19 '05 #1
6 2323
Make it a Public Shared Global class member.

The Global class gets loaded automatically before each page call, so
whatever is in there will be available to everything else.

You could also put the value into the Cahce object.

"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I want to take a global variable in my ASP.NET application. How do I
make it global so that it is accessible to all the pages in my
application? Do I put it in the Global.asax file? If so, Global.asax
now has a class Global and so if I put it there it won't be global
anymore. It'll be a class member. Then how do I access it?

Nov 19 '05 #2
With a web app, generally your global variables should be stored in the
Application Object or Cache object. This way they are explicity made
accessible to all code in your app. Here's an example

'To store a variable:
Application.Lock()
Application("MyVar")="whatever"
Application.Unlock()

'to retrieve the variable:
Dim s as String = Application("MyVar").ToString()

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
I want to take a global variable in my ASP.NET application. How do I
make it global so that it is accessible to all the pages in my
application? Do I put it in the Global.asax file? If so, Global.asax
now has a class Global and so if I put it there it won't be global
anymore. It'll be a class member. Then how do I access it?

Nov 19 '05 #3
But I want to put in a connection object there. And if I make the
connection object static, while I will have it strongly typed, I might
risk the connection being closed by a different thread.

Actually, I want to share the connection, and for that matter more
variables some of which are value types, only accross all my
user-defined class instances but only within a single request.

One option I have is to put it in a dictionary like the Session object.
But I am wondering if there is another way that I can have the
variable/object stored as a strongly-typed reference rather than as a
late bound reference.

Nov 19 '05 #4
WJ
I like static/singleton class that hosts all static variables/data/const.
Then have each page references the class. Example:
myGlobleClass.MyConnectionString.

John
Nov 19 '05 #5
No, don't put a database connection in there, you'll muck up the
scalability.
ADO.NET has built in connection pooling, so you should use it.
Open a connection right before you use it, and close it immediately
afterward. The build in connection pooling actually makes this a lot more
efficient than you might have thought.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net

"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
But I want to put in a connection object there. And if I make the
connection object static, while I will have it strongly typed, I might
risk the connection being closed by a different thread.

Actually, I want to share the connection, and for that matter more
variables some of which are value types, only accross all my
user-defined class instances but only within a single request.

One option I have is to put it in a dictionary like the Session object.
But I am wondering if there is another way that I can have the
variable/object stored as a strongly-typed reference rather than as a
late bound reference.

Nov 19 '05 #6
I am confused by your response.

What is a strongly typed connection? I think you are confusing early
binding and strongly typed (2 different things).

Regardless, connections should not generally be global to begin with. .NET
uses connection pooling, so even if you create and de-reference the
connection object in several different pages, the same connection object
will get reused (this allows you to open and close as you need to).

If you want shared value types, then create them in Global, as I said.
Storing them in Session kind of defeats the purpose of shared members.

Again, you can always use the Cache object.

"Water Cooler v2" <wt*****@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
But I want to put in a connection object there. And if I make the
connection object static, while I will have it strongly typed, I might
risk the connection being closed by a different thread.

Actually, I want to share the connection, and for that matter more
variables some of which are value types, only accross all my
user-defined class instances but only within a single request.

One option I have is to put it in a dictionary like the Session object.
But I am wondering if there is another way that I can have the
variable/object stored as a strongly-typed reference rather than as a
late bound reference.

Nov 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
7
by: djc | last post by:
I have several subroutines (all inline code) that wind up using the same database connection object variable. I have been declaring a new variable in every sub. I just now came to a point where I...
2
by: Water Cooler v2 | last post by:
I want to take a global variable in my ASP.NET application. How do I make it global so that it is accessible to all the pages in my application? Do I put it in the Global.asax file? If so,...
4
by: David Wheeler | last post by:
4.8-STABLE FreeBSD 4.8-STABLE i386 sahlins# cd postgresql-7.4.2/contrib/pg_autovacuum sahlins# make "../../src/Makefile.global", line 38: Missing dependency operator...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
12
by: a | last post by:
def fn(): for i in range(l) global count count= .... how do i declare count to be global if it is an array subsequently i should access or define count as an array error:
2
by: sairam | last post by:
I have some local variables defined in one method and Can I make those variables as global variables? If so , can any one explain me how can I do that Thanks, Sairam
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.