Accessing application variable from static method

  • Thread starter Thread starter DDE
  • Start date Start date
D

DDE

Hi All,

Reformulating a problem from a previous post:

How can I access an application variable from a static method?
When I try:
string myString = Application{"thisString"].ToString;

whithi a static method, I get the message:
CS01020 "In order to use a non-static field, method, or property, you
must first create an object instance.

How can I solve this?

Thanks

Dominique
PS: Anybody knows of a good C# mailing list ?
 
DDE,

The reason this fails is because Application is really a property
exposed by the page object. In order to use it in a static method, you will
have to pass it into the static method (taking a type of
HttpApplicationState), or you can pass the Page object (which exposes the
Application property), or you can use the static Current property on the
HttpContext class, which will expose an Application property which you can
use to get to the application.

Hope this helps.
 
Hi,

thanks for your answer. It is what I suspected. In this case I am working in
a Web Service and not a web page, what should be the type to use to pass it
to my method?

Thanks
Dominique


Nicholas Paldino said:
DDE,

The reason this fails is because Application is really a property
exposed by the page object. In order to use it in a static method, you will
have to pass it into the static method (taking a type of
HttpApplicationState), or you can pass the Page object (which exposes the
Application property), or you can use the static Current property on the
HttpContext class, which will expose an Application property which you can
use to get to the application.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi All,

Reformulating a problem from a previous post:

How can I access an application variable from a static method?
When I try:
string myString = Application{"thisString"].ToString;

whithi a static method, I get the message:
CS01020 "In order to use a non-static field, method, or property, you
must first create an object instance.

How can I solve this?

Thanks

Dominique
PS: Anybody knows of a good C# mailing list ?
 
Dominique,

You can use the third option I gave to you, which would be to use the
Application property exposed by the HttpContext instance returned by the
static Current method on the HttpContext class.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi,

thanks for your answer. It is what I suspected. In this case I am working in
a Web Service and not a web page, what should be the type to use to pass it
to my method?

Thanks
Dominique


in message news:[email protected]...
DDE,

The reason this fails is because Application is really a property
exposed by the page object. In order to use it in a static method, you will
have to pass it into the static method (taking a type of
HttpApplicationState), or you can pass the Page object (which exposes the
Application property), or you can use the static Current property on the
HttpContext class, which will expose an Application property which you can
use to get to the application.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

DDE said:
Hi All,

Reformulating a problem from a previous post:

How can I access an application variable from a static method?
When I try:
string myString = Application{"thisString"].ToString;

whithi a static method, I get the message:
CS01020 "In order to use a non-static field, method, or property, you
must first create an object instance.

How can I solve this?

Thanks

Dominique
PS: Anybody knows of a good C# mailing list ?
 
Back
Top