By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Global's non static methods

P: n/a
A.M
Hi,

Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?

I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic
field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}
Nov 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
That makes sense.

You have to instantiate an instance of type Global before you can reference
a non-static method.

Global g = new Global();
Response.Write( g.CurrentDisplayName );
or you can declare CurrentDisplayName as static and you won't need to
instantiate an object of type Global to reference it.

public static string CurrentDisplayName
{
//your implementation.
}

Response.Write( Global.CurrentDisplayName );

HTH,

bill

"A.M" <IH*******@sapm123.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
Hi,

Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?

I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic
field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}

Nov 18 '05 #2

P: n/a
A.M
I don't think we can create our own instance of Global object.

I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to have
access to that instance by using Page.ApplicationInstance property, But it
doesn't work for me. That means I don't have access to non-static members!
and they don't appear in IDE inlisense

Alan

"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:eq**************@TK2MSFTNGP09.phx.gbl...
That makes sense.

You have to instantiate an instance of type Global before you can reference a non-static method.

Global g = new Global();
Response.Write( g.CurrentDisplayName );
or you can declare CurrentDisplayName as static and you won't need to
instantiate an object of type Global to reference it.

public static string CurrentDisplayName
{
//your implementation.
}

Response.Write( Global.CurrentDisplayName );

HTH,

bill

"A.M" <IH*******@sapm123.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
Hi,

Can Global object have non-static methods?
If answer is yes, then How can I access them in pages?

I have following property in Global.asax.cs, but when I try to use it in
pages, I receive error "An object reference is required for the nonstatic field, method, or property CurrentDisplayName":
Thanks,
Alan
public string CurrentDisplayName
{
get
{
string s;
try
{
s = Session[LLCConstants.SESSION_INDEX_DISPLAYNAME].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
set
{
Session[LLCConstants.SESSION_INDEX_DISPLAYNAME] = value;
}
}


Nov 18 '05 #3

P: n/a
Hi Alan,

On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
wrote:

I don't think we can create our own instance of Global object.

That's correct.
I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to have
access to that instance by using Page.ApplicationInstance property, But it
doesn't work for me. That means I don't have access to non-static members!
and they don't appear in IDE inlisense


I think that is a bug in the documentation. It should probably read
"every page includes a strongly-typed Context property of type
"HttpContext". To get to the instance of the Global class associated
with your request, you can do:

Global g = Context.ApplicationInstance as Global;
if(g != null)
{
// party on g
}

Hope that helps.

--
Scott
http://www.OdeToCode.com
Nov 18 '05 #4

P: n/a
You are both incorrect, you can instantiate an instance of Global.

(How else does asp.net work if it is impossible to instantiate an Global to
process each request?)

But the second part of Scott's reply is correct. I didn't even realize
there was an ApplicationInstance property of the Context.

Alan, Is there any reason you don't want the property to be static?

bill
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:1l********************************@4ax.com...
Hi Alan,

On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
wrote:

I don't think we can create our own instance of Global object.


That's correct.
I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to have
access to that instance by using Page.ApplicationInstance property, But itdoesn't work for me. That means I don't have access to non-static members!and they don't appear in IDE inlisense


I think that is a bug in the documentation. It should probably read
"every page includes a strongly-typed Context property of type
"HttpContext". To get to the instance of the Global class associated
with your request, you can do:

Global g = Context.ApplicationInstance as Global;
if(g != null)
{
// party on g
}

Hope that helps.

--
Scott
http://www.OdeToCode.com

Nov 18 '05 #5

P: n/a
Hi Bill:

Good point. I was looking at it from the view point of that an
instance of Global isn't very useful without ASP.NET wiring up all the
properties and event handlers for you.

--
Scott
http://www.OdeToCode.com

On Wed, 5 May 2004 12:51:33 -0500, "William F. Robertson, Jr."
<wf*********@kpmg.com> wrote:
You are both incorrect, you can instantiate an instance of Global.

(How else does asp.net work if it is impossible to instantiate an Global to
process each request?)

But the second part of Scott's reply is correct. I didn't even realize
there was an ApplicationInstance property of the Context.

Alan, Is there any reason you don't want the property to be static?

bill


Nov 18 '05 #6

P: n/a
A.M

I should say it is not usefull to create an instance of Global class. You
are right, There is no reason for not being able to have an instance from
non-abstarct class!!

The reason that I can't use static properties is I can't have access to
current session object in Global's static methods.

Now I am implimenting that functionality into page template class which I
think is a better approach.

Thanks,
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
You are both incorrect, you can instantiate an instance of Global.

(How else does asp.net work if it is impossible to instantiate an Global to process each request?)

But the second part of Scott's reply is correct. I didn't even realize
there was an ApplicationInstance property of the Context.

Alan, Is there any reason you don't want the property to be static?

bill
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:1l********************************@4ax.com...
Hi Alan,

On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
wrote:

I don't think we can create our own instance of Global object.


That's correct.
I am sure that ASP.NET framework creates the instance for us.
Based on what microsoft mentioned in kb#312607, I should be able to haveaccess to that instance by using Page.ApplicationInstance property, But itdoesn't work for me. That means I don't have access to non-static members!and they don't appear in IDE inlisense


I think that is a bug in the documentation. It should probably read
"every page includes a strongly-typed Context property of type
"HttpContext". To get to the instance of the Global class associated
with your request, you can do:

Global g = Context.ApplicationInstance as Global;
if(g != null)
{
// party on g
}

Hope that helps.

--
Scott
http://www.OdeToCode.com


Nov 18 '05 #7

P: n/a
FYI,

HttpContext.Current.Session can be access by a static method.

public string CurrentDisplayName
{
get
{
string s;
try
{
s =
System.Web.HttpContext.Current.Session[LLCConstants.SESSION_INDEX_DISPLAYNAM
E].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
}

But you are correct, it is a better approach to implement in a page class.
All of the pages on my site derive from my own class that derives from
System.Web.UI.Page and implements some custom interfaces.

HTH
bill
MCSD

"A.M" <IH*******@sapm123.com> wrote in message
news:ub*************@TK2MSFTNGP09.phx.gbl...

I should say it is not usefull to create an instance of Global class. You
are right, There is no reason for not being able to have an instance from
non-abstarct class!!

The reason that I can't use static properties is I can't have access to
current session object in Global's static methods.

Now I am implimenting that functionality into page template class which I
think is a better approach.

Thanks,
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
You are both incorrect, you can instantiate an instance of Global.

(How else does asp.net work if it is impossible to instantiate an Global

to
process each request?)

But the second part of Scott's reply is correct. I didn't even realize
there was an ApplicationInstance property of the Context.

Alan, Is there any reason you don't want the property to be static?

bill
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:1l********************************@4ax.com...
Hi Alan,

On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
wrote:
>I don't think we can create our own instance of Global object.
>

That's correct.

>I am sure that ASP.NET framework creates the instance for us.
>Based on what microsoft mentioned in kb#312607, I should be able to have >access to that instance by using Page.ApplicationInstance property,
But it
>doesn't work for me. That means I don't have access to non-static

members!
>and they don't appear in IDE inlisense
>

I think that is a bug in the documentation. It should probably read
"every page includes a strongly-typed Context property of type
"HttpContext". To get to the instance of the Global class associated
with your request, you can do:

Global g = Context.ApplicationInstance as Global;
if(g != null)
{
// party on g
}

Hope that helps.

--
Scott
http://www.OdeToCode.com



Nov 18 '05 #8

P: n/a
A.M

Ah!

That HttpContext.Current always confuses me!! It is the second time that I
am missing it so badly! The problem is I know what it does but I don't
underestand how HttpContext.Current knows what is the current context.

Thanks for help
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
FYI,

HttpContext.Current.Session can be access by a static method.

public string CurrentDisplayName
{
get
{
string s;
try
{
s =
System.Web.HttpContext.Current.Session[LLCConstants.SESSION_INDEX_DISPLAYNAM E].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
}

But you are correct, it is a better approach to implement in a page class.
All of the pages on my site derive from my own class that derives from
System.Web.UI.Page and implements some custom interfaces.

HTH
bill
MCSD

"A.M" <IH*******@sapm123.com> wrote in message
news:ub*************@TK2MSFTNGP09.phx.gbl...

I should say it is not usefull to create an instance of Global class. You
are right, There is no reason for not being able to have an instance from non-abstarct class!!

The reason that I can't use static properties is I can't have access to
current session object in Global's static methods.

Now I am implimenting that functionality into page template class which I think is a better approach.

Thanks,
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
You are both incorrect, you can instantiate an instance of Global.

(How else does asp.net work if it is impossible to instantiate an Global
to
process each request?)

But the second part of Scott's reply is correct. I didn't even

realize there was an ApplicationInstance property of the Context.

Alan, Is there any reason you don't want the property to be static?

bill
"Scott Allen" <bitmask@[nospam].fred.net> wrote in message
news:1l********************************@4ax.com...
> Hi Alan,
>
> On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
> wrote:
>
>
> >I don't think we can create our own instance of Global object.
> >
>
> That's correct.
>
> >I am sure that ASP.NET framework creates the instance for us.
> >Based on what microsoft mentioned in kb#312607, I should be able to

have
> >access to that instance by using Page.ApplicationInstance property,

But it
> >doesn't work for me. That means I don't have access to non-static
members!
> >and they don't appear in IDE inlisense
> >
>
> I think that is a bug in the documentation. It should probably read
> "every page includes a strongly-typed Context property of type
> "HttpContext". To get to the instance of the Global class associated
> with your request, you can do:
>
> Global g = Context.ApplicationInstance as Global;
> if(g != null)
> {
> // party on g
> }
>
> Hope that helps.
>
> --
> Scott
> http://www.OdeToCode.com
>
>



Nov 18 '05 #9

P: n/a
..net magic?

:)

bill

"A.M" <IH*******@sapm123.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

Ah!

That HttpContext.Current always confuses me!! It is the second time that I
am missing it so badly! The problem is I know what it does but I don't
underestand how HttpContext.Current knows what is the current context.

Thanks for help
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:e4**************@TK2MSFTNGP09.phx.gbl...
FYI,

HttpContext.Current.Session can be access by a static method.

public string CurrentDisplayName
{
get
{
string s;
try
{
s =

System.Web.HttpContext.Current.Session[LLCConstants.SESSION_INDEX_DISPLAYNAM
E].ToString();
}
catch (Exception e)
{
s = "?";
}
return s;
}
}

But you are correct, it is a better approach to implement in a page class.
All of the pages on my site derive from my own class that derives from
System.Web.UI.Page and implements some custom interfaces.

HTH
bill
MCSD

"A.M" <IH*******@sapm123.com> wrote in message
news:ub*************@TK2MSFTNGP09.phx.gbl...

I should say it is not usefull to create an instance of Global class. You are right, There is no reason for not being able to have an instance from non-abstarct class!!

The reason that I can't use static properties is I can't have access to current session object in Global's static methods.

Now I am implimenting that functionality into page template class which I
think is a better approach.

Thanks,
Alan


"William F. Robertson, Jr." <wf*********@kpmg.com> wrote in message
news:OE**************@TK2MSFTNGP10.phx.gbl...
> You are both incorrect, you can instantiate an instance of Global.
>
> (How else does asp.net work if it is impossible to instantiate an Global to
> process each request?)
>
> But the second part of Scott's reply is correct. I didn't even realize > there was an ApplicationInstance property of the Context.
>
> Alan, Is there any reason you don't want the property to be static?
>
> bill
>
>
> "Scott Allen" <bitmask@[nospam].fred.net> wrote in message
> news:1l********************************@4ax.com...
> > Hi Alan,
> >
> > On Wed, 5 May 2004 12:03:00 -0400, "A.M" <IH*******@sapm123.com>
> > wrote:
> >
> >
> > >I don't think we can create our own instance of Global object.
> > >
> >
> > That's correct.
> >
> > >I am sure that ASP.NET framework creates the instance for us.
> > >Based on what microsoft mentioned in kb#312607, I should be able

to have
> > >access to that instance by using Page.ApplicationInstance property, But
> it
> > >doesn't work for me. That means I don't have access to non-static
> members!
> > >and they don't appear in IDE inlisense
> > >
> >
> > I think that is a bug in the documentation. It should probably

read > > "every page includes a strongly-typed Context property of type
> > "HttpContext". To get to the instance of the Global class associated > > with your request, you can do:
> >
> > Global g = Context.ApplicationInstance as Global;
> > if(g != null)
> > {
> > // party on g
> > }
> >
> > Hope that helps.
> >
> > --
> > Scott
> > http://www.OdeToCode.com
> >
> >
>
>



Nov 18 '05 #10

P: n/a
In Win32 there is a method for storing data specific to a thread
called thread local storage. So you can imagine "data" hanging off
each thread. No two threads would share the same instance of a thread
local variable. Where ever the thread goes, be it methods in your code
or methods in a third party library, the data 'follows' along, and an
API call allows code to dig out the thread specific data.

In .NET you can also allocate thread-specific fields with the
ThreadStatic attribute.

So, the above is just some background information on how
HttpContext.Current might know how to know what "current" is, since
each request is paired with one thread from the thread pool.

If you peek inside the property, you'll see Current uses a
CallContext object from the System.Runtime.Remoting.Messaging
namespace. CallContext is thread local storage with some additional
magic added: if a call leaves an application (appdomain), say, through
remoting, than the CallContext object can generate information to
follow along with the remote call. So, in the docs for CallContext
you'll see it can follow a "logical thread of execution", not just a
physical thread like TLS.

There is a little more information in the following article, as well
as some examples of pratices you don't want to do with
HttpContext.Current :)
http://odetocode.com/Articles/112.aspx

-- s
On Wed, 5 May 2004 16:00:04 -0400, "A.M" <IH*******@sapm123.com>
wrote:

Ah!

That HttpContext.Current always confuses me!! It is the second time that I
am missing it so badly! The problem is I know what it does but I don't
underestand how HttpContext.Current knows what is the current context.

Thanks for help
Alan


Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.