473,405 Members | 2,272 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,405 software developers and data experts.

Global's non static methods

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
10 2330
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
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
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
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
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
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
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
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
..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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
2
by: Abdessamad Belangour | last post by:
Hi all, and thanks for previous answers (especially Nicholas Paldino) An assembly is composed of a set of modules. The module class has a method for reading global methods GetMethods(). My...
10
by: David P. Donahue | last post by:
When I wrote websites in VB .NET, I would often put functions in Global for all the pages to call. Now, in C#, doing so results in "references to non-static objects" and whatnot. I realize what...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
24
by: LP | last post by:
After a code review one coworker insisted that global are very dangerous. He didn't really give any solid reasons other than, "performance penalties", "hard to maintain", and "dangerous". I think...
17
by: Mike L | last post by:
This is for a Win form. Currently I have several connection strings that are identical through out my application. Is a global variable the best choice for this situation? If so, what is the...
8
by: Morpheus | last post by:
Hello, Say I have a class with a member... char mId; Whenever an object is created, I want to assign an incrementing character to this member, for instance the first would be A, then B, C,...
3
by: RP | last post by:
In VB we used to add a module that contained public variables, procedures and functions. Where to declare global variables and functions in C# so that they are accessible everywhere.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.