Global's non static methods

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;
}
}
 
W

William F. Robertson, Jr.

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

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 said:
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 said:
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;
}
}
 
S

Scott Allen

Hi Alan,

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.
 
W

William F. Robertson, Jr.

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
 
S

Scott Allen

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.
 
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
 
W

William F. Robertson, Jr.

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

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 said:
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 said:
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




Global
to
 
W

William F. Robertson, Jr.

..net magic?

:)

bill

A.M said:
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 said:
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 said:
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 property,
But
 
S

Scott Allen

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top