Connecting Tech Pros Worldwide Help | Site Map

An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'

jake
Guest
 
Posts: n/a
#1: Nov 21 '08
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.

My simplified code that generates the error is:

public static string getCartCookieValue()
{

if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}

DH
Guest
 
Posts: n/a
#2: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


jake wrote:
Quote:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
>
My simplified code that generates the error is:
>
public static string getCartCookieValue()
{
>
if (Request.Cookies["tepcart"] != null)
{
using (CartServiceClient client = new CartServiceClient())
{
retval = client.getCartId(Request.Cookies["tepcart"].Value);
client.Close();
}
}
return retval;
}
>
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
jake
Guest
 
Posts: n/a
#3: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


I am sorry. Slip of the keyboard. I modified the message a bit.
Please refer to my latest version of the problem above.
jake

On Nov 21, 3:44*pm, DH <I.dont.n...@more.spamwrote:
Quote:
jake wrote:
Quote:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
>
Quote:
My simplified code that generates the error is:
>
Quote:
public static string getCartCookieValue()
{
>
Quote:
* *if (Request.Cookies["tepcart"] != null)
* *{
* * * * * *using (CartServiceClient client = new CartServiceClient())
* * * * * *{
* * * * * * * * * *retval = client.getCartId(Request.Cookies["tepcart"].Value);
* * * * * * * * * *client.Close();
* * * * * *}
* *}
* *return retval;
}
>
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
jake
Guest
 
Posts: n/a
#4: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.

The error is the one mention in the title of this message.
Thanks,
jake


On Nov 21, 3:44*pm, DH <I.dont.n...@more.spamwrote:
Quote:
jake wrote:
Quote:
I think I may why this is happening but I am a little fuzzy as to what
the solution should be as I am somewhat new to all of this.
>
Quote:
My simplified code that generates the error is:
>
Quote:
public static string getCartCookieValue()
{
>
Quote:
* *if (Request.Cookies["tepcart"] != null)
* *{
* * * * * *using (CartServiceClient client = new CartServiceClient())
* * * * * *{
* * * * * * * * * *retval = client.getCartId(Request.Cookies["tepcart"].Value);
* * * * * * * * * *client.Close();
* * * * * *}
* *}
* *return retval;
}
>
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
DH
Guest
 
Posts: n/a
#5: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


jake wrote:
Quote:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
>
The error is the one mention in the title of this message.
Thanks,
jake
>
>
On Nov 21, 3:44 pm, DH <I.dont.n...@more.spamwrote:
Quote:
>jake wrote:
Quote:
>>I think I may why this is happening but I am a little fuzzy as to what
>>the solution should be as I am somewhat new to all of this.
>>My simplified code that generates the error is:
>>public static string getCartCookieValue()
>>{
>> if (Request.Cookies["tepcart"] != null)
>> {
>> using (CartServiceClient client = new CartServiceClient())
>> {
>> retval = client.getCartId(Request.Cookies["tepcart"].Value);
>> client.Close();
>> }
>> }
>> return retval;
>>}
>where are you declaring retval? if you are declaring it inside of the
>using statement it is becuase the scope of it is just inside of the
>using statment.
>what is the exact error you are getting?
>
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.
jake
Guest
 
Posts: n/a
#6: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


On Nov 21, 4:10*pm, DH <I.dont.n...@more.spamwrote:
Quote:
jake wrote:
Quote:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
>
Quote:
The error is the one mention in the title of this message.
Thanks,
jake
>
Quote:
On Nov 21, 3:44 pm, DH <I.dont.n...@more.spamwrote:
Quote:
jake wrote:
>I think I may why this is happening but I am a little fuzzy as to what
>the solution should be as I am somewhat new to all of this.
>My simplified code that generates the error is:
>public static string getCartCookieValue()
>{
>* *if (Request.Cookies["tepcart"] != null)
>* *{
>* * * * * *using (CartServiceClient client = new CartServiceClient())
>* * * * * *{
>* * * * * * * * * *retval = client.getCartId(Request.Cookies["tepcart"].Value);
>* * * * * * * * * *client.Close();
>* * * * * *}
>* *}
>* *return retval;
>}
where are you declaring retval? if you are declaring it inside of the
using statement it is becuase the scope of it is just inside of the
using statment.
what is the exact error you are getting?
>
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.
It is a .Net class. A simple web page actually. I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now. The
"Request.get" method is a non-static method that can only be in other
non-static methods. I guess the question now is: Is there another way
to get the cookies other than via the Request object?
Ignacio Machin ( .NET/ C# MVP )
Guest
 
Posts: n/a
#7: Nov 21 '08

re: An object reference is required for the non-static field, method, orproperty 'System.Web.UI.Page.Request.get'


On Nov 21, 4:35 pm, jake <jakedim...@gmail.comwrote:
Quote:
On Nov 21, 4:10 pm, DH <I.dont.n...@more.spamwrote:
>
>
>
Quote:
jake wrote:
Quote:
If I may add that the error does not occur and it executes properly
when I make the getCartCookie() an instance method rather than a
static method.
>
Quote:
Quote:
The error is the one mention in the title of this message.
Thanks,
jake
>
Quote:
Quote:
On Nov 21, 3:44 pm, DH <I.dont.n...@more.spamwrote:
>jake wrote:
>>I think I may why this is happening but I am a little fuzzy as to what
>>the solution should be as I am somewhat new to all of this.
>>My simplified code that generates the error is:
>>public static string getCartCookieValue()
>>{
>> if (Request.Cookies["tepcart"] != null)
>> {
>> using (CartServiceClient client = new CartServiceClient())
>> {
>> retval = client.getCartId(Request.Cookies["tepcart"].Value);
>> client.Close();
>> }
>> }
>> return retval;
>>}
>where are you declaring retval? if you are declaring it inside of the
>using statement it is becuase the scope of it is just inside of the
>using statment.
>what is the exact error you are getting?
>
Quote:
Are you using a .Net class or your own class? Either way there is a
difference between static and non-static methods.
Google "C# static vs Non static" and there are tons of articles if you
are not familiar with it.
The reason you get that error when you dont make getCartCookie() an
instance is becuase the methods are not static which means they are
instance methods. If it is your class that you are using you might be
able to get away with making them static but you need to look at the
overall picture and see if it is a good idea or not. I dont recommend
doing that if other project and classes use it unless you really have to.
>
It is a .Net class. A simple web page actually. I was only slightly
fuzzy on the concept and I did a little quick reading after your last
message and I think I understand it much better now. The
"Request.get" method is a non-static method that can only be in other
non-static methods. I guess the question now is: Is there another way
to get the cookies other than via the Request object?
Why you declare it as static in the first place?

In case you REALLY need it to be static you can use HttpContext:
HttpContext.Current.Request

Using HttpContext is very usefull when you have DLLs that are not
part of the web project. In those cases using HttpContext is the only
way to access the request

Closed Thread