Connecting Tech Pros Worldwide Forums | Help | Site Map

Sharing a variable between all pages

oliv@linuxmail.org
Guest
 
Posts: n/a
#1: Apr 17 '07
Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?


public partial class _Default : System.Web.UI.Page
{

static public string sharedvar;

protected void Page_Load(object sender, EventArgs e)
{
...

thanks


Marc Gravell
Guest
 
Posts: n/a
#2: Apr 17 '07

re: Sharing a variable between all pages


When you say "does not seem to always work" - what happens?

Either way, a static field is a risky proposition in a highly threaded
environment such as ASP.Net; at the very least this should be
protected by a property and synchronised between threads. There may be
more appropriate solutions: what are you trying to do with this var?
What does it hold?

Marc

=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
 
Posts: n/a
#3: Apr 17 '07

re: Sharing a variable between all pages


This is probably more of an ASP.NET question than a C# language group one.
Typically in ASP.NET you would store your item either in Session,
Application, or Cache state depending on the business scenario or whether it
is user-specific.
Alternatively, you can put a public static field in your Global class
(global.asax) and you can then use it from any page with:

Global.MyItemName;

Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net




"oliv@linuxmail.org" wrote:
Quote:
Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
>
>
public partial class _Default : System.Web.UI.Page
{
>
static public string sharedvar;
>
protected void Page_Load(object sender, EventArgs e)
{
...
>
thanks
>
>
Peter Bradley
Guest
 
Posts: n/a
#4: Apr 17 '07

re: Sharing a variable between all pages


Yeah. The thing is, the Session is more for maintaining state across
different pages associated with the same Session. Perhaps the OP could
explain if these pages are sharing a Session - or if they are in different
Sessions (i.e. a second instance in a second browser instance on the same
client - or even two separate browser instances on different machines with
potentially different users). Until the OP gives us a bit more info about
what is required, it's hard to know what the exact nature of the problem is.


Peter


"Peter Bromberg [C# MVP]" <pbromberg@yahoo.yabbadabbadoo.comwrote in
message news:5ED4E954-84D3-4BEE-BC23-A8C0A4D2994B@microsoft.com...
Quote:
This is probably more of an ASP.NET question than a C# language group one.
Typically in ASP.NET you would store your item either in Session,
Application, or Cache state depending on the business scenario or whether
it
is user-specific.
Alternatively, you can put a public static field in your Global class
(global.asax) and you can then use it from any page with:
>
Global.MyItemName;
>
Peter
>
--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net
>
>
>
>
"oliv@linuxmail.org" wrote:
>
Quote:
>Hi,
>New to .NET, I was wondering what was the proper way to share a
>variable between all the instances of a web page.
>I try with a static var, but it does not seem to always work. Why is
>that ?
>>
>>
>public partial class _Default : System.Web.UI.Page
>{
>>
> static public string sharedvar;
>>
> protected void Page_Load(object sender, EventArgs e)
> {
> ...
>>
>thanks
>>
>>

oliv@linuxmail.org
Guest
 
Posts: n/a
#5: Apr 17 '07

re: Sharing a variable between all pages


could you provide sample code for storing a var at an application
scope ? (not session scope)

(by "it does not seem to always work", I mean that the static var has
been set by some instance of the page, and another read a null value -
this behavior is different from java as to me)

Jay Warmack
Guest
 
Posts: n/a
#6: Apr 18 '07

re: Sharing a variable between all pages


Sounds like the Singleton pattern is in order here. Google 'Singleton
pattern in C#' and you should find countless examples of the middle tier
code required to implement the concept of global variables in a web or
windows form applcation.


<oliv@linuxmail.orgwrote in message
news:1176815115.879181.30580@b58g2000hsg.googlegro ups.com...
Quote:
Hi,
New to .NET, I was wondering what was the proper way to share a
variable between all the instances of a web page.
I try with a static var, but it does not seem to always work. Why is
that ?
>
>
public partial class _Default : System.Web.UI.Page
{
>
static public string sharedvar;
>
protected void Page_Load(object sender, EventArgs e)
{
...
>
thanks
>

Closed Thread